std::basic_string::capacity
size_type capacity() const; | | |
---|
返回字符串当前为其分配空间的字符数。
参数
%280%29
返回值
当前分配的存储空间的容量。
例外
(none) | (until C++11) |
---|---|
noexcept specification: noexcept | (since C++11) |
复杂性
常量。
例
二次
#include <iostream>
#include <string>
void show_capacity(std::string const& s)
{
std::cout << "'" << s << "' has capacity " << s.capacity() << ".\n";
}
int main()
{
std::string s{"Exemplar"};
show_capacity(s);
s += " is an example string.";
show_capacity(s);
}
二次
可能的产出:
二次
'Exemplar' has capacity 8.
'Exemplar is an example string.' has capacity 30.
二次
另见
sizelength | returns the number of characters (public member function) |
---|---|
reserve | reserves storage (public member function) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com