Skip to Content

<std::vector> not only stores the elements; it also stores the size of the vector:


The .size() function returns the number of elements in the vector.

For example, suppose we have a std::string vector with Sonny’s grocery list:

std::vector<std::string> grocery = {"Hot Pepper Jam", "Dragon Fruit", "Brussel Sprouts"};

It should look something like this:

  • The string at index 0 is "Hot Pepper Jam".
  • The string at index 1 is "Dragon Fruit".
  • The string at index 2 is "Brussel Sprouts".
std::cout << grocery.size() << "\n";

will return


Notice how nothing goes in the parentheses.



Add a few more items to the grocery list using .push_back().

Print the size of grocery using .size().

Is it the same as you expected?

Folder Icon

Take this course for free

Already have an account?