Namespaces
Variants

std::basic_string<CharT,Traits,Allocator>::basic_string

来自 zh.cppreference.net
 
 
 
std::basic_string
 
basic_string() : basic_string(Allocator()) {}
(1) (C++11 起)
(C++17 前)
basic_string() noexcept(noexcept(Allocator()))
    : basic_string(Allocator()) {}
(C++17 起)
(C++20 起 constexpr)
explicit basic_string( const Allocator& alloc = Allocator() );
(2) (C++11 前)
explicit basic_string( const Allocator& alloc );
(C++11 起)
(C++17 起 noexcept)
(C++20 起 constexpr)
basic_string( size_type count, CharT ch,
              const Allocator& alloc = Allocator() );
(3) (C++20 起 constexpr)
template< class InputIt >
basic_string( InputIt first, InputIt last,
              const Allocator& alloc = Allocator() );
(4) (C++20 起 constexpr)
template< container-compatible-range<CharT> R >
constexpr basic_string( std::from_range_t, R&& rg,
                        const Allocator& = Allocator());
(5) (C++23 起)
basic_string( const CharT* s, size_type count,
              const Allocator& alloc = Allocator() );
(6) (C++20 起 constexpr)
basic_string( const CharT* s, const Allocator& alloc = Allocator() );
(7) (C++20 起 constexpr)
basic_string( std::nullptr_t ) = delete;
(8) (C++23 起)
template< class StringViewLike >
explicit basic_string( const StringViewLike& t,
                       const Allocator& alloc = Allocator() );
(9) (C++17 起)
(C++20 起 constexpr)
template< class StringViewLike >
basic_string( const StringViewLike& t,
              size_type pos, size_type count,
              const Allocator& alloc = Allocator() );
(10) (C++17 起)
basic_string( const basic_string& other );
(11) (C++20 起 constexpr)
basic_string( basic_string&& other ) noexcept;
(12) (C++11 起)
(C++20 起 constexpr)
basic_string( const basic_string& other, const Allocator& alloc );
(13) (C++11 起)
(C++20 起 constexpr)
basic_string( basic_string&& other, const Allocator& alloc );
(14) (C++11 起)
(C++20 起 constexpr)
basic_string( const basic_string& other, size_type pos,
              const Allocator& alloc = Allocator() );
(15) (C++20 起 constexpr)
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,
              const Allocator& alloc = Allocator() );
(17) (C++20 起 constexpr)
constexpr basic_string( basic_string&& other,
                        size_type pos, size_type count,
                        const Allocator& alloc = Allocator() );
(18) (C++23 起)
basic_string( std::initializer_list<CharT> ilist,
              const Allocator& alloc = Allocator() );
(19) (C++11 起)
(C++20 起 constexpr)

从多种数据源构造新字符串,并可选择使用用户提供的分配器alloc。

1) 自 C++11 起的默认构造函数。构造一个空字符串,并使用默认构造的分配器。
如果 Allocator 不是可默认构造型,则行为未定义。
2) C++11 前的默认构造函数。构造一个空字符串,并使用给定的分配器alloc。
3) 构造一个字符串,包含字符count的ch份副本。

如果CharT不是可复制插入型到std::basic_string<CharT>中,则行为未定义。

(C++11 起)

仅当Allocator满足Allocator的要求时,此重载才会参与重载决议。

(C++17 起)
4) 构造一个字符串,内容为范围[first, last)中的元素。[first, last)中的每个迭代器恰好被解引用一次。

如果InputIt不满足老式输入迭代器的要求,则转而调用重载(3),参数为static_cast<size_type>(first)、last和alloc。

(C++11 前)

仅当InputIt满足老式输入迭代器的要求时,此重载才会参与重载决议。

如果CharT不是可原位构造型从std::basic_string<CharT>中的*first,则行为未定义。

(C++11 起)
5) 构造一个字符串,内容为范围rg中的元素。rg中的每个迭代器恰好被解引用一次。
如果CharT不是可原位构造型从std::basic_string<CharT>中的*ranges::begin(rg),则行为未定义。
6) 构造一个字符串,内容为范围[s, s + count)中的元素。
如果[s, s + count)不是一个有效的范围,则行为未定义。
7) 等价于basic_string(s, Traits::length(s), alloc)。

仅当Allocator满足Allocator的要求时,此重载才会参与重载决议。

(C++17 起)
8) std::basic_string 不能从nullptr构造。
9) 隐式地将t转换为字符串视图sv,如同通过std::basic_string_view<CharT, Traits> sv = t;,然后如同通过basic_string(sv.data(), sv.size(), alloc)构造一个字符串。
仅当std::is_convertible_v<const StringViewLike&,
std::basic_string_view<CharT, Traits>>
是true且std::is_convertible_v<const StringViewLike&, const CharT*>是false时,此重载才会参与重载决议。
10) 隐式地将t转换为字符串视图sv,如同通过std::basic_string_view<CharT, Traits> sv = t;,然后如同通过basic_string(sv.substr(pos, n), alloc)构造一个字符串。
仅当std::is_convertible_v<const StringViewLike&,
std::basic_string_view<CharT, Traits>>
是true时,此重载才会参与重载决议。
11-18) 用(部分)other的内容构造一个字符串。如果other的类型是basic_string&&,则构造完成后,other处于有效但未指定的状态。
11) 复制构造函数。

分配器如同通过调用std::allocator_traits<Allocator>::
select_on_container_copy_construction
(other.get_allocator())
获取。

(C++11 起)
12) 移动构造函数。分配器通过从other.get_allocator()进行移动构造获得。
13) 与复制构造函数相同,但使用alloc作为分配器。
如果CharT不是可复制插入型到std::basic_string<CharT>中,则行为未定义。
14) 与移动构造函数相同,但使用alloc作为分配器。
如果CharT不是可移动插入型到std::basic_string<CharT>中,则行为未定义。
15,16) 构造一个字符串,内容为范围[other.data() + pos, other.data() + other.size())中的元素。
17,18) 构造一个字符串,内容为范围[other.data() + pos, other.data() + (pos + std::min(count, other.size() - pos)))中的元素。
19) 等价于basic_string(ilist.begin(), ilist.end())。

参数

alloc - 用于此字符串所有内存分配的分配器
count - 结果字符串的大小
ch - 用于初始化字符串的值
pos - 要包含的第一个字符的位置
first, last - 要复制字符的范围
s - 用作字符串初始化源的字符数组指针
other - 用作字符串初始化源的另一个字符串
ilist - std::initializer_list 用于初始化字符串
t - 可转换为 std::basic_string_view 的对象,用于初始化字符串
rg - 兼容的容器范围

复杂度

1,2) 常量。
3-7) 与字符串长度呈线性关系。
9-11) 与字符串长度呈线性关系。
12) 常量。
13) 与字符串长度呈线性关系。
14) 当 alloc ! = other. get_allocator ( ) 为 true 时,时间复杂度与字符串大小成线性关系,否则为常数时间复杂度。
15-19) 与字符串长度呈线性关系。

异常

10) std::out_of_range 若 pos 超出范围。
14) 当 alloc == str. get_allocator ( ) 为 true 时不抛出任何异常。
15-18) std::out_of_range 若 pos > other. size ( ) 为 true 。

若构造的字符串长度超过 std::length_error (例如对于 (3) 当 count > max_size ( ) 时)。对 Allocator::allocate 的调用可能抛出异常。

若因任何原因抛出异常,这些函数将不产生任何效果( 强异常安全保证 )。

注释

使用包含嵌入 ' \0 ' 字符的 字符串字面量 进行初始化时,会使用重载 (7) ,该重载在遇到第一个空字符时停止读取。可通过指定其他构造函数或使用 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"
功能测试 宏 值 标准 功能
__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'; // 未指定
    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", 即 [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", 即 [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)

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用版本 发布时行为 正确行为
LWG 301 C++98 重载 (4) 在 InputIt 为整型时未使用参数
alloc
使用该参数
LWG 438 C++98 重载 (4) 仅在 InputIt 为整型时调用重载 (3) 当 InputIt 不是 LegacyInputIterator 时
调用重载 (3)
LWG 847 C++98 未提供异常安全保证 增加强异常安全保证
LWG 2193 C++11 默认构造函数为 explicit 改为非 explicit
LWG 2235 C++98 s 可能为空指针值 此情况下行为未定义
LWG 2250 C++98 当 pos > other. size ( ) 为 true 时
重载 (17) 行为未定义
此情况下始终抛出
异常
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]
  1. ↑ [ nullptr , nullptr + 0 ) 是一个有效的空范围,因为将零与空指针值相加同样是 良好定义的 (结果仍为空指针值)。

参见

为字符串分配字符
(公开成员函数)
为字符串赋值
(公开成员函数)
(C++11)
将整型或浮点型值转换为 string
(函数)
(C++11)
将整型或浮点型值转换为 wstring
(函数)
构造 basic_string_view
( std::basic_string_view<CharT,Traits> 的公开成员函数)