它确定向量中元素的数量。
int n=v.size();
它返回向量中元素的数量。
让我们看一个简单的例子。
#include<iostream> #include<vector> using namespace std; int main() { vector<string> v{"Welcome to learnfk","c"}; int n=v.size(); cout<<"Size of the string is :"<<n; return 0; }
输出:
Size of the string is:2
在此示例中,包含两个字符串和size()函数的向量v给出了向量中元素的数量。
让我们看另一个简单的例子。
#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 of the vector is :5
在此示例中,包含整数值和size()函数的向量v确定向量中的元素数。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)