3.3.3. Other vector Operations

Image

In addition to push_back, vector provides only a few other operations, most of which are similar to the corresponding operations on strings. Table 3.5 lists the most important ones.

Table 3.5. vector Operations

Image

We access the elements of a vector the same way that we access the characters in a string: through their position in the vector. For example, we can use a range for3.2.3, p. 91) to process all the elements in a vector:

vector<int> v{1,2,3,4,5,6,7,8,9};for (auto &i : v)     // for each element in v (note: ...

Get C++ Primer, Fifth Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.