C++ vector
In this page:
#include <vector>
std::vector<data_type> vector_name = {value1, value2};
vector_name.push_back(value);
vector_name.pop_back();
vector_name[index];
What is a vector?
A vector is C++'s dynamic array template class: unlike a fixed-size raw array, a vector automatically grows or shrinks as elements are added or removed, managing its own underlying heap memory without you having to call new or delete yourself.
Example: What is a vector?
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums;
nums.push_back(1);
nums.push_back(2);
// Print to the console with cout
std::cout << nums.size() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Adding and Removing Elements
You add an element to the end of a vector with push_back() and remove the last element with pop_back(); both operations automatically adjust the vector's reported size, so you never manually track how many elements it currently holds.
Example: Adding and Removing Elements
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums = {1, 2};
nums.push_back(3);
nums.pop_back();
// Print to the console with cout
std::cout << nums.size() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Accessing Elements
You can access an element with plain index brackets like vec[i], or with the at() method, which is safer because it performs bounds checking and throws an out_of_range exception rather than silently reading invalid memory for a bad index.
Example: Accessing Elements
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums = {10, 20, 30};
// Print to the console with cout
std::cout << nums[1] << " " << nums.at(2) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Vector Capacity vs Size
Size is the number of elements a vector actually currently holds, while capacity is the total memory it has reserved, which is often larger.
Vectors deliberately over-allocate capacity in advance so that most push_back() calls don't need to trigger an expensive reallocation.
Example: Vector Capacity vs Size
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums = {1, 2, 3};
// Print to the console with cout
std::cout << "size=" << nums.size() << " capacity=" << nums.capacity() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Iterating over Vectors
You can iterate over a vector's elements with a traditional index-based loop, a range-based for loop for cleaner syntax, or explicit STL iterators — all three work interchangeably and the choice mostly comes down to readability and whether you need the index itself.
Example: Iterating over Vectors
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums = {1, 2, 3};
// Loop: repeat while the condition holds
for (int i = 0; i < nums.size(); i++) std::cout << nums[i] << " ";
for (int n : nums) std::cout << n << " ";
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using
v[i]past the end, which is undefined behavior;v.at(i)throws instead. - Keeping an iterator or reference after
push_backcauses the vector to reallocate, which invalidates it. - Confusing
size()(elements stored) withcapacity()(space reserved).
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: