C++ How to get the vector size?
:sptth//www.theitroad.com
To get the size of a vector in C++, you can use the size()
method. Here's an example:
#include <iostream> #include <vector> int main() { std::vector<int> v {1, 2, 3, 4, 5}; // get the size of the vector std::cout << "The size of the vector is: " << v.size() << std::endl; return 0; }
Output:
The size of the vector is: 5
In the above example, we first initialize a vector v
with 5 elements. We then use the size()
method to get the number of elements in the vector, which is 5 in this case. The size()
method returns an unsigned integer, which represents the number of elements in the vector.