.size()
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 integersstd::vector<int> numbers = {1, 2, 3, 4};// Print out vector sizestd::cout << numbers.size();}
The output of the above code is:
4
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours