std::basic_string<CharT,Traits,Allocator>::assign_range
来自 cppreference.cn
< cpp | string | basic_string
template< container-compatible-range<CharT> R > constexpr std::basic_string& assign_range( R&& rg ); |
(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。
如果由于任何原因抛出异常,此函数无效果(强异常安全保证)。
[编辑] 注解
特性测试宏 | 值 | 标准 | 特性 |
---|---|---|---|
__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"); }
[编辑] 参阅
给字符串赋值字符 (public member function) | |
给字符串赋值 (public member function) | |
构造一个 basic_string (public member function) |