首页
学习
活动
专区
圈层
工具
发布

std::basic_string::capacity

size_type capacity() const;

返回字符串当前为其分配空间的字符数。

参数

%280%29

返回值

当前分配的存储空间的容量。

例外

(none)

(until C++11)

noexcept specification: noexcept

(since C++11)

复杂性

常量。

二次

代码语言:javascript
代码运行次数:0
复制
#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);
}

二次

可能的产出:

二次

代码语言:javascript
代码运行次数:0
复制
'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)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券