admin管理员组文章数量:1611941
stl vector 函数
C ++ vector :: capacity()函数 (C++ vector::capacity() function)
vector::capacity() is a library function of "vector" header, it is used to find the capacity of a vector, it returns the storage space currently allocated to the vector.
vector :: capacity()是“ vector”头文件的库函数,用于查找向量的容量,它返回当前分配给该向量的存储空间。
Note: To use vector, include <vector> header.
注意:要使用向量,请包含<vector>标头。
Syntax of vector::capacity() function
vector :: capacity()函数的语法
vector::capacity();
Parameter(s): void – It accepts nothing as a parameter.
参数: void –不接受任何参数。
Return value: size_type – It returns capacity i.e. storage space of a vector.
返回值: size_type –返回容量,即向量的存储空间。
Example:
例:
Input:
vector<int> vector1{ 1, 2, 3, 4, 5 };
Function call:
cout << vector1.capacity();
Output:
8
C ++程序演示vector :: capacity()函数的示例 (C++ program to demonstrate example of vector::capacity() function)
//C++ STL program to demonstrate example of
//vector::capacity() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1;
//printing the size & capacity of the vector
cout << "Total number of elements: " << v1.size() << endl;
cout << "Storage space: " << v1.capacity() << endl;
//pushing elements
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
//printing the size & capacity of the vector
cout << "Total number of elements: " << v1.size() << endl;
cout << "Storage space: " << v1.capacity() << endl;
return 0;
}
Output
输出量
Total number of elements: 0
Storage space: 0
Total number of elements: 5
Storage space: 8
Reference: C++ vector::capacity()
参考: C ++ vector :: capacity()
翻译自: https://www.includehelp/stl/vector-capacity-function-with-example.aspx
stl vector 函数
版权声明:本文标题:stl vector 函数_vector :: capacity()函数以及C ++ STL中的示例 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1728620280a1166267.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论