std::basic_string<CharT,Traits,Allocator>::basic_string
(1) | ||
basic_string() : basic_string(Allocator()) {} |
(自 C++11 起) (直到 C++17) |
|
basic_string() noexcept(noexcept(Allocator())) : basic_string(Allocator()) {} |
(自 C++17 起) (constexpr 自 C++20 起) |
|
(2) | ||
explicit basic_string( const Allocator& alloc = Allocator() ); |
(直到 C++11) | |
explicit basic_string( const Allocator& alloc ); |
(noexcept 自 C++17 起) (constexpr 自 C++20 起) |
|
basic_string( size_type count, CharT ch, const Allocator& alloc = Allocator() ); |
(3) | (constexpr 自 C++20 起) |
template< class InputIt > basic_string( InputIt first, InputIt last, |
(4) | (constexpr 自 C++20 起) |
template< container-compatible-range<CharT> R > constexpr basic_string( std::from_range_t, R&& rg, |
(5) | (自 C++23 起) |
basic_string( const CharT* s, size_type count, const Allocator& alloc = Allocator() ); |
(6) | (constexpr 自 C++20 起) |
basic_string( const CharT* s, const Allocator& alloc = Allocator() ); |
(7) | (constexpr 自 C++20 起) |
basic_string( std::nullptr_t ) = delete; |
(8) | (自 C++23 起) |
template< class StringViewLike > explicit basic_string( const StringViewLike& t, |
(9) | (自 C++17 起) (constexpr 自 C++20 起) |
template< class StringViewLike > basic_string( const StringViewLike& t, |
(10) | (自 C++17 起) |
basic_string( const basic_string& other ); |
(11) | (constexpr 自 C++20 起) |
basic_string( basic_string&& other ) noexcept; |
(12) | (自 C++11 起) (constexpr 自 C++20 起) |
basic_string( const basic_string& other, const Allocator& alloc ); |
(13) | (自 C++11 起) (constexpr 自 C++20 起) |
basic_string( basic_string&& other, const Allocator& alloc ); |
(14) | (自 C++11 起) (constexpr 自 C++20 起) |
basic_string( const basic_string& other, size_type pos, const Allocator& alloc = Allocator() ); |
(15) | (constexpr 自 C++20 起) |
constexpr basic_string( basic_string&& other, size_type pos, const Allocator& alloc = Allocator() ); |
(16) | (自 C++23 起) |
basic_string( const basic_string& other, size_type pos, size_type count, |
(17) | (constexpr 自 C++20 起) |
constexpr basic_string( basic_string&& other, size_type pos, size_type count, |
(18) | (自 C++23 起) |
basic_string( std::initializer_list<CharT> ilist, const Allocator& alloc = Allocator() ); |
(19) | (自 C++11 起) (constexpr 自 C++20 起) |
从各种数据源构造新的字符串,并可选择使用用户提供的分配器 alloc。
如果 |
(自 C++11 起) |
仅当 |
(自 C++17 起) |
[
first,
last)
。范围 [
first,
last)
中的每个迭代器都恰好被解引用一次。
如果 |
(直到 C++11) |
仅当 如果 |
(自 C++11 起) |
[
s,
s + count)
。[
s,
s + count)
不是有效范围,则行为未定义。
仅当 |
(自 C++17 起) |
std::basic_string
不能从 nullptr 构造。std::basic_string_view<CharT, Traits>> 为 true 且 std::is_convertible_v<const StringViewLike&, const CharT*> 为 false 时,此重载才参与重载决议。
std::basic_string_view<CharT, Traits>> 为 true 时,此重载才参与重载决议。
basic_string&&
,则构造完成后,other 处于有效但不确定的状态。
分配器通过调用 std::allocator_traits<Allocator>:: |
(自 C++11 起) |
[
other.data() + pos,
other.data() + other.size())
。[
other.data() + pos,
other.data() + (pos + std::min(count, other.size() - pos)))
。内容 |
[编辑] 参数
alloc | - | 用于此字符串的所有内存分配的分配器 |
count | - | 结果字符串的大小 |
ch | - | 用于初始化字符串的值 |
pos | - | 要包含的第一个字符的位置 |
first, last | - | 从中复制字符的范围 |
s | - | 指向字符数组的指针,用作初始化字符串的源 |
other | - | 另一个字符串,用作初始化字符串的源 |
ilist | - | 用于初始化字符串的 std::initializer_list |
t | - | 对象(可转换为 std::basic_string_view),用于初始化字符串 |
rg | - | 容器兼容范围 |
[编辑] 复杂度
[编辑] 异常
如果构造的字符串长度将超过 max_size(),则抛出 std::length_error 异常(例如,对于 (3),如果 count > max_size())。对 Allocator::allocate
的调用可能会抛出异常。
如果由于任何原因抛出异常,这些函数都不会产生任何影响(强异常安全保证)。
[编辑] 注解
使用包含嵌入 '\0' 字符的 字符串字面量 初始化会使用重载 (7),它会在第一个空字符处停止。这可以通过指定不同的构造函数或使用 operator""s
来避免
std::string s1 = "ab\0\0cd"; // s1 contains "ab" std::string s2{"ab\0\0cd", 6}; // s2 contains "ab\0\0cd" std::string s3 = "ab\0\0cd"s; // s3 contains "ab\0\0cd"
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_containers_ranges |
202202L |
(C++23) | 标记构造函数 (5),用于从 容器兼容范围 构造 |
[编辑] 示例
#include <cassert> #include <cctype> #include <iomanip> #include <iostream> #include <iterator> #include <string> int main() { std::cout << "1) string(); "; std::string s1; assert(s1.empty() && (s1.length() == 0) && (s1.size() == 0)); std::cout << "s1.capacity(): " << s1.capacity() << '\n'; // unspecified std::cout << "3) string(size_type count, CharT ch): "; std::string s2(4, '='); std::cout << std::quoted(s2) << '\n'; // "====" std::cout << "4) string(InputIt first, InputIt last): "; char mutable_c_str[] = "another C-style string"; std::string s4(std::begin(mutable_c_str) + 8, std::end(mutable_c_str) - 1); std::cout << std::quoted(s4) << '\n'; // "C-style string" std::cout << "6) string(CharT const* s, size_type count): "; std::string s6("C-style string", 7); std::cout << std::quoted(s6) << '\n'; // "C-style", i.e. [0, 7) std::cout << "7) string(CharT const* s): "; std::string s7("C-style\0string"); std::cout << std::quoted(s7) << '\n'; // "C-style" std::cout << "11) string(string&): "; std::string const other11("Exemplar"); std::string s11(other11); std::cout << std::quoted(s11) << '\n'; // "Exemplar" std::cout << "12) string(string&&): "; std::string s12(std::string("C++ by ") + std::string("example")); std::cout << std::quoted(s12) << '\n'; // "C++ by example" std::cout << "15) string(const string& other, size_type pos): "; std::string const other15("Mutatis Mutandis"); std::string s15(other15, 8); std::cout << std::quoted(s15) << '\n'; // "Mutandis", i.e. [8, 16) std::cout << "17) string(const string& other, size_type pos, size_type count): "; std::string const other17("Exemplary"); std::string s17(other17, 0, other17.length() - 1); std::cout << std::quoted(s17) << '\n'; // "Exemplar" std::cout << "19) string(std::initializer_list<CharT>): "; std::string s19({'C', '-', 's', 't', 'y', 'l', 'e'}); std::cout << std::quoted(s19) << '\n'; // "C-style" }
可能的输出
1) string(); s1.capacity(): 15 3) string(size_type count, CharT ch): "====" 4) string(InputIt first, InputIt last): "C-style string" 6) string(CharT const* s, size_type count): "C-style" 7) string(CharT const* s): "C-style" 11) string(string&): "Exemplar" 12) string(string&&): "C++ by example" 15) string(const string& other, size_type pos): "Mutandis" 17) string(const string& other, size_type pos, size_type count): "Exemplar" 19) string(std::initializer_list<CharT>): "C-style"
[编辑] 缺陷报告
以下行为变更的缺陷报告已追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 301 | C++98 | 重载 (4) 未使用参数 alloc (如果 InputIt 是整型) |
使用该参数 |
LWG 438 | C++98 | 重载 (4) 仅调用重载 (3) 如果 InputIt 是整型 |
如果 InputIt 不是 LegacyInputIterator,则调用重载 (3)不是 LegacyInputIterator |
LWG 847 | C++98 | 没有异常安全保证 | 添加了强异常安全保证 |
LWG 2193 | C++11 | 默认构造函数是显式的 | 变为非显式 |
LWG 2235 | C++98 | s 可以是空指针值 | 在这种情况下,行为未定义 |
LWG 2250 | C++98 | 如果 pos > other.size() 为 true,则重载 (17) 的行为未定义 如果 pos > other.size() 为 true |
始终抛出一个 在这种情况下抛出异常 |
LWG 2583 | C++98 | 没有办法为重载 (17) 提供分配器 | 添加了重载 (15) |
LWG 2946 | C++17 | 在某些情况下,重载 (9) 引起歧义 | 通过使其成为模板来避免歧义 |
LWG 3076 | C++17 | 重载 (3,7) 可能会导致歧义 在类模板参数推导中 |
已约束 |
LWG 3111 (P1148R0) |
C++98 | LWG issue 2235 的解决方案使 basic_string(nullptr, 0) 的行为未定义 |
在这种情况下,行为是良好定义的 定义的[1] |
[编辑] 参见
向字符串赋值字符 (公共成员函数) | |
向字符串赋值 (公共成员函数) | |
(C++11) |
将整型或浮点型值转换为 string (函数) |
(C++11) |
将整型或浮点型值转换为 wstring (函数) |
构造一个 basic_string_view ( std::basic_string_view<CharT,Traits> 的公共成员函数) |