C++ Vector size
It calculates the quantity of items in the vector.
Syntax
Consider a vector 'v' and a count of elements 'n'. The syntax is as follows:
int n=v.size();
Parameter
It does not contain any parameter.
Return value
It returns the number of elements in the vector.
Example 1
Let's see a simple example.
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<string> v{"Welcome to javaCppTutorial","c"};
int n=v.size();
cout<<"Size of the string is :"<<n;
return 0;
}
Output:
Size of the string is:2
In this instance, vector v holds two strings, and the size method returns the count of elements within the vector.
Example 2
Let's see a another simple example.
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v{1,2,3,4,5};
int n=v.size();
cout<<"Size of the vector is :"<<n;
return 0;
}
Output:
Size of the vector is :5
In this instance, vector v holds integer values and the size method calculates the total number of elements stored in the vector.