std::basic_string<CharT,Traits,Allocator>::capacity

来自cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
size_type capacity() const;
(C++11 前)
size_type capacity() const noexcept;
(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 15.
'Exemplar is an example string.' has capacity 31.

参阅

返回字符数
(公开成员函数)
保留存储
(公开成员函数)