previous | start | next

## Vector Subscripts

- You can find the size vector by calling the `size` function.

  >     for(i = 0; i < v.size(); i++)
  >        do something with v[i];

- Using `size` is actually better than using a magic number (see Quality Tip 2.3).
- The function `push_back` allows you to start out with an empty vector and grow the vector whenever another element is added.

  >     vector<double> salaries;
  >     . . .
  >     double s;
  >     cin >> s;
  >     . . .
  >     salaries.push_back(s);

- The `push_back` command resizes the vector by adding one element to its end.
- If you already know how many elements you need in a vector, you should specify that size when you define it.
- Another member function, `pop_back`, removes the last element of a vector, shrinking its size by one.

  >     salaries.pop_back(); /* Now salaries has size 9 */

- The standard defines many more useful functions for vectors; in this book, we only use `push_back` and `pop_back`.

previous | start | next