Tutorial by Examples

A std::vector can be initialized in several ways while declaring it: C++11 std::vector<int> v{ 1, 2, 3 }; // v becomes {1, 2, 3} // Different from std::vector<int> v(3, 6) std::vector<int> v{ 3, 6 }; // v becomes {3, 6} // Different from std::vector<int> v{3, ...
Appending an element at the end of a vector (by copying/moving): struct Point { double x, y; Point(double x, double y) : x(x), y(y) {} }; std::vector<Point> v; Point p(10.0, 2.0); v.push_back(p); // p is copied into the vector. C++11 Appending an element at the end of a vector ...
You can iterate over a std::vector in several ways. For each of the following sections, v is defined as follows: std::vector<int> v; Iterating in the Forward Direction C++11 // Range based for for(const auto& value: v) { std::cout << value << "\n"; } /...
There are two primary ways of accessing elements in a std::vector index-based access iterators Index-based access: This can be done either with the subscript operator [], or the member function at(). Both return a reference to the element at the respective position in the std::vector (unles...
There are several ways to use a std::vector as a C array (for example, for compatibility with C libraries). This is possible because the elements in a vector are stored contiguously. C++11 std::vector<int> v{ 1, 2, 3 }; int* p = v.data(); In contrast to solutions based on previous C++ st...
Iterators and pointers pointing into an std::vector can become invalid, but only when performing certain operations. Using invalid iterators/pointers will result in undefined behavior. Operations which invalidate iterators/pointers include: Any insertion operation which changes the capacity of...
Deleting the last element: std::vector<int> v{ 1, 2, 3 }; v.pop_back(); // v becomes {1, 2} Deleting all elements: std::vector<int> v{ 1, 2, 3 }; v.clear(); // v becomes an empty vector Deleting element by index: std::vect...
The function std::find, defined in the <algorithm> header, can be used to find an element in a std::vector. std::find uses the operator== to compare elements for equality. It returns an iterator to the first element in the range that compares equal to the value. If the element in question is...
An array can easily be converted into a std::vector by using std::begin and std::end: C++11 int values[5] = { 1, 2, 3, 4, 5 }; // source array std::vector<int> v(std::begin(values), std::end(values)); // copy array to new vector for(auto &x: v) std::cout << x << &q...
The standard (section 23.3.7) specifies that a specialization of vector<bool> is provided, which optimizes space by packing the bool values, so that each takes up only one bit. Since bits aren't addressable in C++, this means that several requirements on vector are not placed on vector<bool...
Vector size is simply the number of elements in the vector: Current vector size is queried by size() member function. Convenience empty() function returns true if size is 0: vector<int> v = { 1, 2, 3 }; // size is 3 const vector<int>::size_type size = v.size(); cout << size...
One std::vector can be append to another by using the member function insert(): std::vector<int> a = {0, 1, 2, 3, 4}; std::vector<int> b = {5, 6, 7, 8, 9}; a.insert(a.end(), b.begin(), b.end()); However, this solution fails if you try to append a vector to itself, because the sta...
A std::vector automatically increases its capacity upon insertion as needed, but it never reduces its capacity after element removal. // Initialize a vector with 100 elements std::vector<int> v(100); // The vector's capacity is always at least as large as its size auto const old_capacity...
The <algorithm> header provides a number of useful functions for working with sorted vectors. An important prerequisite for working with sorted vectors is that the stored values are comparable with <. An unsorted vector can be sorted by using the function std::sort(): std::vector<int&...
C++11 In C++11, compilers are required to implicitly move from a local variable that is being returned. Moreover, most compilers can perform copy elision in many cases and elide the move altogether. As a result of this, returning large objects that can be moved cheaply no longer requires special ha...
To find the largest or smallest element stored in a vector, you can use the methods std::max_element and std::min_element, respectively. These methods are defined in <algorithm> header. If several elements are equivalent to the greatest (smallest) element, the methods return the iterator to th...
Vectors can be used as a 2D matrix by defining them as a vector of vectors. A matrix with 3 rows and 4 columns with each cell initialised as 0 can be defined as: std::vector<std::vector<int> > matrix(3, std::vector<int>(4)); C++11 The syntax for initializing them using initia...

Page 1 of 1