{ using alloc_t = DebugAllocator<char>; using str_t = std::basic_string< char, std::char_traits<char>, alloc_t >; std::cout << "sizeof(std::string) : " << sizeof(str_t) << std::endl; str_t s{}; s += "1234567890"; s += "abcde"; std::cout << "capacity : "<...
0x100007708 <+24>: bl 0x100006908 ; std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__is_long() const 0x10000770c <+28>: tbz w0, #0x0, 0x100007724 ; <+52> 0x100007710 <+32>: b 0x100007714 ; <+36> 0x100007714 <+36>: ldr...
示例运行此代码 #include <string> #include <iostream> int main() { std::string foo("quuuux"); char bar[7]{}; foo.copy(bar, sizeof bar); std::cout << bar << '\n'; } 输出: quuuux参阅substr 返回子串 (公开成员函数)
std::basic_string<CharT,Traits,Allocator>::resize (1) voidresize(size_type count); (C++20 前) constexprvoidresize(size_type count); (C++20 起) (2) voidresize(size_type count, CharT ch); (C++20 前) constexprvoidresize(size_type count, CharT ch);...
可能抛出任何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...
1) 告诉std::basic_string 对象大小的有计划更改,使得它能准确地管理存储分配。若new_cap 大于当前 capacity() ,则分配新存储,并令 capacity() 大于或等于 new_cap。 若new_cap 小于当前 capacity() ,则这是非强制的收缩请求。 若new_cap 小于当前 size() ,则这是非强制的收缩到适合 (shrink-to-fit)...
对[0, size()]中每个i有data()+i==std::addressof(operator[](i))。 (C++11 起) 复杂度 常数。 示例 运行此代码 #include <algorithm>#include <cassert>#include <cstring>#include <string>intmain(){std::stringconsts("Emplary");assert(s.size()==std::strlen(s.data()));assert(std::equ...
重设string 大小以含 count 个字符。 若当前大小小于 count ,则后附额外的字符。 若当前大小大于 count ,则缩减 string 到为其首 count 个元素。 第一版本初始化新字符为 CharT() ,第二版本初始化新字符为 ch。 参数count - string 的新大小 ch - 用以初始化新字符的字符 ...
std::string采用std::allocator<char>作为分配器,由_Compressed_pair的EBO得,分配器并不会占用内存空间。该分配作用于std::_Is_simple_alloc_v<std::_Rebind_alloc_t<std::allocator<char>, char>>为true,因此std::string的内存布局可以拆解如下 // std::string同一时间只可能是短字符串或长字符串 ...
std::string 简单入门 string的定义原型 typedef basic_string<char, char_traits<char>, allocator<char> > string; typedef basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > wstring; // 第二三个参数有默认值 string部分构造方式...