可能抛出任何std::allocator_traits<Allocator>::allocate()所抛的异常,如std::bad_alloc。 复杂度 至多与 string 的size()成线性 示例 运行此代码 #include <cassert>#include <string>intmain(){std::strings;std::string::size_typenew_capacity{100u};assert(new_capacity>s.capacity());s.reserve(new...
std::basic_string<CharT,Traits,Allocator>::capacity 返回当前已为字符串分配空间的字符数。 参数 (无) 返回值 当前分配的存储,即可用于存储元素的存储的容量。 复杂度 常数 注解 从分配器获得,但不可用于存储任何元素的内存位置不计入分配的存储。注意空终止符不是basic_string的元素。
1) 告诉std::basic_string 对象大小的有计划更改,使得它能准确地管理存储分配。若new_cap 大于当前 capacity() ,则分配新存储,并令 capacity() 大于或等于 new_cap。 若new_cap 小于当前 capacity() ,则这是非强制的收缩请求。 若new_cap 小于当前 size() ,则这是非强制的收缩到适合 (shrink-to-fit)...
#include <string> #include <iostream> int main() { std::string a = "0123456789abcdefghij"; // count 为 npos ,返回 [pos, size()) std::string sub1 = a.substr(10); std::cout << sub1 << '\n'; // pos 和 pos+count 都在边界内,返回 [pos, pos+count) std::string sub2 = ...
basic_string::allocator_type 显示另外 61 个 由basic_string 类型的一个对象控制的序列是标准 C++ 字符串类且作为字符串被引用,但不应将它们与以 null 结尾的通用于 C++ 标准库的 C 样式字符串相混淆。 标准 C++ 字符串是一个容器,它可使字符串作为普通类型使用,例如,比较和连接操作、迭代器、C++ 标准库...
该类型是模板参数的 Allocator同义词。 示例 复制 // basic_string_allocator_type.cpp // compile with: /EHsc #include <string> #include <iostream> int main( ) { using namespace std; // The following lines declare objects // that use the default allocator. string s1; basic_string <char>...
含子串 [pos, pos+count) 的string 。 异常 若pos >size() 则为std::out_of_range 复杂度 与count 成线性 注解 如同以 basic_string(data()+pos, count) 构造返回的 string ,这隐含将会默认构造返回的 string 的分配器——新分配器将不是this->get_allocator() 的副本。
注意空终止符不是 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);...
可能抛出任何 std::allocator_traits<Allocator>::allocate() 抛出的异常,如 std::bad_alloc。 如果因为任何原因抛出了异常,那么此函数无效果(强异常安全保证)。 复杂度至多与字符串的 size() 成线性。 示例运行此代码 #include <cassert> #include <iostream> #include <string> int main() { std::string...
std::basic_string::find_first_not_of std::basic_string::find_first_of std::basic_string::find_last_not_of std::basic_string::find_last_of std::basic_string::front std::basic_string::get_allocator std::basic_string::insert std::basic_string::length std::basic_string::max_size std...