std::basic_string::basic_string
来自cppreference.com
                    
                                        
                    < cpp | string | basic string
                    
                                                            
                    | (1) | ||
|   explicit basic_string( const Allocator& alloc = Allocator() );  | 
(C++14 前) | |
|   basic_string() : basic_string( Allocator() ) {} explicit basic_string( const Allocator& alloc );  | 
 (C++14 起)  (C++17 前)  | 
|
|   basic_string() noexcept(noexcept( Allocator() )): basic_string( Allocator() ) {} explicit basic_string( const Allocator& alloc ) noexcept;  | 
(C++17 起) | |
|   basic_string( size_type count,                CharT ch,   | 
(2) | |
| (3) | ||
|   basic_string( const basic_string& other,                size_type pos,   | 
(C++17 前) | |
|   basic_string( const basic_string& other,               size_type pos,  | 
(C++17 起) | |
|   basic_string( const basic_string& other,                size_type pos,   | 
(C++17 起) | |
|   basic_string( const CharT* s,               size_type count,   | 
(4) | |
|   basic_string( const CharT* s, const Allocator& alloc = Allocator() );  | 
(5) | |
|   template< class InputIt > basic_string( InputIt first, InputIt last,   | 
(6) | |
|   basic_string( const basic_string& other );  | 
(7) | |
|   basic_string( const basic_string& other, const Allocator& alloc );  | 
(7) | (C++11 起) | 
|   basic_string( basic_string&& other ) noexcept;  | 
(8) | (C++11 起) | 
|   basic_string( basic_string&& other, const Allocator& alloc );  | 
(8) | (C++11 起) | 
|   basic_string( std::initializer_list<CharT> init,  const Allocator& alloc = Allocator() );  | 
(9) | (C++11 起) | 
|   explicit basic_string( std::basic_string_view<CharT, Traits> sv, const Allocator& alloc = Allocator() );  | 
(10) | (C++17 起) | 
|   template < class T > basic_string( const T& t, size_type pos, size_type n,  | 
(11) | (C++17 起) | 
从各种数据源构造新 string ,可选地使用用户提供的分配器 alloc 。
1) 默认构造函数。构造空 string (零大小和未指定的容量)。
2) 构造拥有字符 
ch 的 count 个副本的 string 。若 count >= npos 则行为未定义。3) 以 
other 的子串 [pos, pos+count) 构造 string 。若 count == npos 或未指定 count ,或若请求的子串越过字符串的结尾,则产生的子串为 [pos, size()) 。4) 以 
s 所指向的字符串的首 count 个字符构造 string 。 s 能包含空字符。 string 的长度为 count 。若 s 不指向 CharT 的至少 count 个元素的数组,包括 s 是空指针的情况,则行为未定义。5) 以 
s 所指向的空终止字符串的副本所初始化的内容构造 string 。以首个空字符确定字符串的长度。若 s 不指向 CharT 的至少 Traits::length(s)+1 个元素的数组,包括 s 为空指针的情况,则行为未定义。6) 构造拥有范围 
[first, last) 内容的 string 。若 InputIt 为整数类型,则等价于 basic_string(static_cast<size_type>(first), static_cast<value_type>(last), a) 。7) 复制构造函数。构造拥有 
other 内容副本的 string 。8) 移动构造函数。用移动语义构造拥有 
other 内容的 string 。将 other 留在合法但未指定的状态。9) 构造拥有 initializer_list 
init 内容的 string 。10) 构造拥有字符串视图 
sv 内容的 string ,如同用 basic_string(sv.data(), sv.size(), alloc) 。11) 如同用 std::basic_string_view<CharT, Traits> sv = t; 隐式转换 
t 为字符串视图 sv ,然后如同用 basic_string(sv.substr(pos, n), a) ,以 sv 的子范围 [pos, pos + n) 初始化 string 。此重载仅若 std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> 为 true 才参与重载决议。目录 | 
[编辑] 参数
| alloc | - | 用于此 string 所有内存分配的分配器 | 
| count | - | 产生的 string 大小 | 
| ch | - | 初始化 string 所用的值 | 
| pos | - | 要包含的首字符位置 | 
| first, last | - | 复制字符的来源范围 | 
| s | - | 指向用作源初始化 string 的字符数组的指针 | 
| other | - | 用作源初始化 string 的另一 string | 
| init | - | 初始化 string 所用的 std::initializer_list | 
| sv | - | 初始化 string 所用的 std::basic_string_view | 
| t | - | 初始化 string 所用的对象(可转换为 std::basic_string_view ) | 
[编辑] 复杂度
1) 常数
2-4) 与 
count 成线性5) 与 
s 的长度成线性6) 与 
first 和 last 间的距离成线性7) 与 
other 的大小成线性8) 常数。若给出 
alloc 且 alloc != other.get_allocator() 则为线性9) 与 
init 的大小成线性[编辑] 异常
3) 若 pos > other.size() 则为 std::out_of_range
8) 若 alloc == str.get_allocator() 则不抛出
对 Allocator::allocate 的调用可能抛出。
[编辑] 注意
以含有内嵌 '\0' 字符的字符串字面量使用重载 (5) ,它在首个空字符停止。这可通过指定不同的构造函数,或通过使用 operator""s 避免:
std::string s1 = "ab\0\0cd"; // s1 含 "ab" std::string s2{"ab\0\0cd", 6}; // s2 含 "ab\0\0cd" std::string s3 = "ab\0\0cd"s; // s3 含 "ab\0\0cd"
[编辑] 示例
运行此代码
#include <iostream> #include <cassert> #include <iterator> #include <string> #include <cctype> int main() { { // string::string() std::string s; assert(s.empty() && (s.length() == 0) && (s.size() == 0)); } { // string::string(size_type count, charT ch) std::string s(4, '='); std::cout << s << '\n'; // "====" } { std::string const other("Exemplary"); // string::string(string const& other, size_type pos, size_type count) std::string s(other, 0, other.length()-1); std::cout << s << '\n'; // "Exemplar" } { // string::string(charT const* s, size_type count) std::string s("C-style string", 7); std::cout << s << '\n'; // "C-style" } { // string::string(charT const* s) std::string s("C-style\0string"); std::cout << s << '\n'; // "C-style" } { char mutable_c_str[] = "another C-style string"; // string::string(InputIt first, InputIt last) std::string s(std::begin(mutable_c_str)+8, std::end(mutable_c_str)-1); std::cout << s << '\n'; // "C-style string" } { std::string const other("Exemplar"); std::string s(other); std::cout << s << '\n'; // "Exemplar" } { // string::string(string&& str) std::string s(std::string("C++ by ") + std::string("example")); std::cout << s << '\n'; // "C++ by example" } { // string(std::initializer_list<charT> ilist) std::string s({ 'C', '-', 's', 't', 'y', 'l', 'e' }); std::cout << s << '\n'; // "C-style" } { // 重载决议选择 string(InputIt first, InputIt last) [with InputIt = int] // 这表现为如同调用 string(size_type count, charT ch) std::string s(3, std::toupper('a')); std::cout << s << '\n'; // "AAA" } }
输出:
==== Exemplar C-style C-style C-style string Exemplar C++ by example C-style AAA
[编辑] 参阅
|   赋值字符给字符串  (公开成员函数)  | |
|    为字符串赋值   (公开成员函数)  |