C++ Vector size()
C++ Vector size()
它确定 Vector 中元素的数量。
语法
考虑一个 Vector " v"和元素个数" n"。语法为:
参数
它不包含任何参数。
返回值
它返回 Vector 中的元素。
示例1
让我们看一个简单的示例。
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<string> v{"Welcome to lidihuo","c"};
int n=v.size();
cout<<"Size of the string is :"<<n;
return 0;
}
输出:
在此示例中,包含两个字符串和size()函数的 Vector v给出了 Vector 中的元素数量。
示例2
让我们看看另一个简单的例子。
#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;
}
输出:
在此示例中,包含整数值和size()函数的 Vector v确定 Vector 中的元素数。