在线文档教程
C++
字符串 | Strings

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.

二次

另见

sizelengthreturns the number of characters (public member function)
reservereserves storage (public member function)

© cppreference.com

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

http://en.cppreference.com/w/cpp/string/basic[医]字符串/容量