命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
template< container-compatible-range<CharT> R >
constexpr std::basic_string& assign_range( R&& rg );
(since C++23)

用范围 rg 中的值替换字符串的内容。

等效于

return assign(
    std::basic_string(
        std::from_range,
        std​::​forward<R>(rg),
        get_allocator())
);

内容

[编辑] 参数

rg - 容器兼容范围

[编辑] 返回值

*this

[编辑] 复杂度

rg 的大小呈线性关系。

[编辑] 异常

如果操作将导致 size() 超过 max_size(),则抛出 std::length_error

如果因任何原因抛出异常,则此函数无效(强异常安全保证)。

[编辑] 注释

特性测试 Std 特性
__cpp_lib_containers_ranges 202202L (C++23) 接受 容器兼容范围 的成员函数

[编辑] 示例

#include <cassert>
#include <string>
 
int main()
{
    const auto source = {'s', 'o', 'u', 'r', 'c', 'e'};
    std::string destination{"destination"};
 
#ifdef __cpp_lib_containers_ranges
    destination.assign_range(source);
#else
    destination.assign(source.begin(), source.end());
#endif
 
    assert(destination == "source");
}

[编辑] 参见

将字符赋值给字符串
(公有成员函数) [编辑]
将值赋值给字符串
(公有成员函数) [编辑]
构造一个 basic_string
(公有成员函数) [编辑]