std::basic_string<CharT,Traits,Allocator>::capacity
来自cppreference.com
                    
                                        
                    < cpp | string | basic string
                    
                                                            
                    |   size_type capacity() const;  | 
(C++11 前) | |
|   size_type capacity() const noexcept;  | 
 (C++11 起)  (C++20 前)  | 
|
|   constexpr size_type capacity() const noexcept;  | 
(C++20 起) | |
返回当前已为字符串分配空间的字符数。
参数
(无)
返回值
当前分配的存储,即可用于存储元素的存储的容量。
复杂度
常数
注解
从分配器获得,但不可用于存储任何元素的内存位置不计入分配的存储。注意空终止符不是 basic_string 的元素。
示例
运行此代码
#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.
参阅
|   返回字符数  (公开成员函数)  | |
|   保留存储  (公开成员函数)  |