C++ vector::capacity() 函数
vector::capacity()是"vector"头的库函数,用于求向量的容量,返回当前分配给向量的存储空间。
注意:要使用矢量,请包括<vector>
标题。
vector::capacity() 函数的语法
vector::capacity();
参数: void
– 它不接受任何参数。
返回值: size_type
– 它返回容量,即向量的存储空间。
例:
Input: vector<int> vector1{ 1, 2, 3, 4, 5 }; Function call: cout << vector1.capacity(); Output: 8
C++程序演示vector::capacity()函数的例子
//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;
}
输出
Total number of elements:0 Storage space:0 Total number of elements:5 Storage space:8
参考:C++ vector::capacity()
相关用法
- C++ vector::crend()用法及代码示例
- C++ vector::cbegin()用法及代码示例
- C++ vector::cend()用法及代码示例
- C++ vector::clear()用法及代码示例
- C++ vector::cbegin()、vector::cend()用法及代码示例
- C++ vector::crbegin()用法及代码示例
- C++ vector::max_size()用法及代码示例
- C++ vector::rbegin()用法及代码示例
- C++ vector::pop_back()用法及代码示例
- C++ vector::push_back()用法及代码示例
- C++ vector::emplace_back用法及代码示例
- C++ vector::at()用法及代码示例
- C++ vector::swap()用法及代码示例
- C++ vector::shrink_to_fit()用法及代码示例
- C++ vector::back()用法及代码示例
- C++ vector::assign()用法及代码示例
- C++ vector::begin()用法及代码示例
- C++ vector::operator[]用法及代码示例
- C++ vector::empty()用法及代码示例
- C++ vector::front()、vector::back()用法及代码示例
注:本文由纯净天空筛选整理自 vector::capacity() function with example in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。