std::vector Reference
advanced
c++17
containers
vector
#include <vector>
A contiguous array type that can grow and shrink in size. Here’s some examples of basic std::vector
operations:
#include <vector> #include <iostream> template <typename ...Args> void println(const Args& ...args) { (std::cout << ... << args) << "\n"; } int main() { std::vector<int> vec; vec.push_back(1); vec.push_back(2); println(vec.size()); println(vec[0]); println("---"); println(vec.back()); vec.pop_back(); println(vec.size()); println("---"); vec[0] = 7; println(vec[0]); println("---"); vec.insert(vec.end(), {1, 2, 3}); for (auto x : vec) { println(x); } }
2 1 --- 2 1 --- 7 --- 7 1 2 3
Construction
Besides the standard default, copy, and move constructors you would expect, std::vector
also has two special constructors.
The first special constructor is the initializer list ({a, b, ...}
) constructor that constructs the vector with a copy of the given list elements.
The second special constructor takes two parameters, a count of elements to create, and a default value to initialize those elements with.
If not provided the default value is a special value, the default initialization, which is actually different than a declaration. Be careful, these constructors can be error prone and a large source of confusion in C++, especially with vectors of integers. Here’s some examples:
#include <vector> #include <iostream> void print_vector(const std::vector<int>& vec) { for (auto it = vec.begin(); it != vec.end() - 1; it++) { std::cout << *it << ", "; } std::cout << vec.back() << "\n"; } int main() { std::vector<int> vecFromList{1, 2, 3, 4}; // Note the "{}" std::vector<int> fourZeroes(4); std::vector<int> sevenEights(7, 8); // Note the "()" print_vector(vecFromList); print_vector(fourZeroes); print_vector(sevenEights); }
1, 2, 3, 4 0, 0, 0, 0 8, 8, 8, 8, 8, 8, 8
Indexing
std::vector
implements the array index operator([]
) meaning you can access values by integer index. Here’s an example:
#include <vector> #include <iostream> int main() { std::vector<int> vec{1, 1, 2, 3, 5, 8}; std::cout << vec[3] << "\n"; }
3
If you try to access an index that isn’t in the vector that is undefined behavior.
There’s no telling what will happen, your program may crash, or it may continue on for some time, but with bad data.
For bounds checked access into std::vector
you can use the at
method which throws an exception when an index is out of bounds.
Here’s an example:
#include <vector> #include <iostream> int main() { std::vector<int> vec{1, 1, 2, 3, 5, 8}; try { vec.at(10); } catch (const std::out_of_range& e) { std::cout << __FILE__ << ":" << __LINE__ << " std::out_of_range: " << e.what() << "\n"; } }
src/vector_reference3.cpp:10 std::out_of_range: vector
Iteration
std::vector
supports both forwards and reverse iteration meaning it has both begin(),
end(),
rbegin(), and
rend()` methods. Here’s some examples:
#include <vector> #include <iostream> int main() { std::vector<int> vec{1, 2, 3, 4}; // range-for for (int n : vec) { std::cout << n << " "; } std::cout << "\n"; // forwards iteration for (auto it = vec.begin(); it != vec.end(); it++) { std::cout << *it << " "; } std::cout << "\n"; // reverse iteration for (auto it = vec.rbegin(); it != vec.rend(); it++) { std::cout << *it << " "; } std::cout << "\n"; }
1 2 3 4 1 2 3 4 4 3 2 1