-
Book Overview & Buying
-
Table Of Contents
C++ STL Cookbook - Second Edition
By :
The vector is one of the most widely used containers in the STL, and for good reason. It's just as convenient as an array but far more powerful and flexible. It's common practice to use the [] operator to access elements in a vector like this:
vector v {19, 71, 47, 192, 4004};
auto& i = v[2];
The vector class also provides a member function for the same purpose:
auto& i = v.at(2);
The result is the same but there is an important difference. The at() function does bounds checking and the [] operator does not. This is intentional, as it allows the [] operator to maintain compatibility with the original C-array. Conversely, the bounds checking of the at() method requires a tiny bit of extra compute time, which may or may not be important in a given application. Let's examine this in somewhat more detail.
There are two ways to access an element with an index in a vector. The at() member function...
Change the font size
Change margin width
Change background colour