std::basic_string<CharT,Traits,Allocator>::assign_range
来自cppreference.com
< cpp | string | basic string
template<container-compatible-range<CharT> R > constexprstd::basic_string& assign_range( R&& rg ); | (C++23 起) | |
以范围 rg 中的值替换字符串的内容。
等价于
return assign(std::basic_string(std::from_range, std::forward<R>(rg), get_allocator()));
目录 |
[编辑]参数
rg | - | container compatible range |
[编辑]返回值
*this
[编辑]复杂度
与 rg 的大小成线性。
[编辑]异常
如果操作会导致 size()
超出 max_size()
,那么就会抛出 std::length_error。
如果因为任何原因抛出了异常,那么此函数无效果(强异常安全保证)。
[编辑]注解
功能特性测试宏 | 值 | 标准 | 功能特性 |
---|---|---|---|
__cpp_lib_containers_ranges | 202202L | (C++23) | 接受容器兼容范围的成员函数 |
[编辑]示例
运行此代码
#include <cassert>#include <string> int main(){constauto 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 (公开成员函数) |