Article by Ayman Alheraki on January 11 2026 10:35 AM
std::vector Functions in Modern C++In this article, we provide a detailed explanation of all the essential functions of the std::vector library in C++ with illustrative examples for each function.
std::vectorpush_backAdds a new element at the end of the vector, increasing its size by one.
int main() { std::vector<int> vec; vec.push_back(10); // Adds element 10 to the end of the vector vec.push_back(20); // Adds element 20 to the end of the vector for (int v : vec) { std::cout << v << " "; // Output: 10 20 } return 0;}emplace_backSimilar to push_back, but constructs the new element directly in place without requiring a copy or move, making it more efficient for complex elements.
int main() { std::vector<std::string> vec; vec.emplace_back("Hello"); // Directly adds an element vec.emplace_back("World"); for (const auto& str : vec) { std::cout << str << " "; // Output: Hello World } return 0;}operator[]Provides access to elements via index, similar to arrays, but without bounds checking.
int main() { std::vector<int> vec = {1, 2, 3}; std::cout << vec[1]; // Output: 2 return 0;}atSimilar to operator[], but performs bounds checking and throws an exception if the index is out of range.
int main() { std::vector<int> vec = {1, 2, 3}; try { std::cout << vec.at(5); // Will throw an exception } catch (const std::out_of_range& e) { std::cout << "Index out of range!"; } return 0;}front and backfront returns the first element.
back returns the last element.
int main() { std::vector<int> vec = {10, 20, 30}; std::cout << "First: " << vec.front(); // Output: First: 10 std::cout << "Last: " << vec.back(); // Output: Last: 30 return 0;}resizeChanges the size of the vector, allowing it to grow or shrink. If the vector is enlarged, new elements are filled with default values.
int main() { std::vector<int> vec = {1, 2, 3}; vec.resize(5); // Resizes to 5, adding default values (0) for (int v : vec) { std::cout << v << " "; // Output: 1 2 3 0 0 } return 0;}reserveReserves space without changing the current size, reducing the number of reallocations required when adding new elements.
int main() { std::vector<int> vec; vec.reserve(10); // Reserves space for 10 elements std::cout << "Capacity: " << vec.capacity(); // Output: Capacity: 10 return 0;}shrink_to_fitReduces the capacity to match the current size, freeing up any unused memory.
int main() { std::vector<int> vec = {1, 2, 3}; vec.reserve(10); // Reserves space for 10 elements vec.shrink_to_fit(); // Shrinks capacity to match the current size std::cout << "Capacity after shrink: " << vec.capacity(); // Output: Capacity after shrink: 3 return 0;}insertInserts an element at a specific position within the vector.
int main() { std::vector<int> vec = {1, 2, 4}; vec.insert(vec.begin() + 2, 3); // Inserts element 3 at index 2 for (int v : vec) { std::cout << v << " "; // Output: 1 2 3 4 } return 0;}eraseRemoves an element or a range of elements from the vector.
int main() { std::vector<int> vec = {1, 2, 3, 4}; vec.erase(vec.begin() + 1); // Removes the element at index 1 for (int v : vec) { std::cout << v << " "; // Output: 1 3 4 } return 0;}clearRemoves all elements from the vector, reducing its size to zero.
int main() { std::vector<int> vec = {1, 2, 3}; vec.clear(); // Removes all elements std::cout << "Size after clear: " << vec.size(); // Output: Size after clear: 0 return 0;}pop_backRemoves the last element in the vector.
int main() { std::vector<int> vec = {1, 2, 3}; vec.pop_back(); // Removes the last element for (int v : vec) { std::cout << v << " "; // Output: 1 2 } return 0;}size and capacitysize: Returns the number of elements in the vector.
capacity: Returns the current capacity of the vector, which is the amount of memory reserved.
int main() { std::vector<int> vec = {1, 2, 3}; std::cout << "Size: " << vec.size() << std::endl; // Output: Size: 3 std::cout << "Capacity: " << vec.capacity() << std::endl; // Output: Capacity: 3 (or more depending on implementation) return 0;}emptyReturns true if the vector is empty; otherwise, it returns false.
int main() { std::vector<int> vec; if (vec.empty()) { std::cout << "Vector is empty!"; } return 0;}The std::vector in C++ offers high flexibility and ease of use. By understanding and utilizing its various functions correctly, one can achieve efficient memory management and high performance in applications that require dynamic data storage.