.size()

eddiepeters00's avatar
Published Aug 6, 2024
Contribute to Docs

The .size() method returns the number of elements in the vector. It follows the consistency of other standard library containers, such as maps and strings.

Syntax

vector.size();

The vector must be defined using std::vector before the .size() method can be used.

Example

In the example below, .size() is called on the numbers vector:

#include <iostream>
#include <vector>
int main() {
// Declaring a vector with 4 integers
std::vector<int> numbers = {1, 2, 3, 4};
// Print out vector size
std::cout << numbers.size();
}

The output of the above code is:

4

All contributors

Contribute to Docs

Learn C++ on Codecademy