std::basic_string<CharT,Traits,Allocator>::assign_range
来自cppreference.com
< 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 | - | container compatible range |
返回值
*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"); }
参阅
赋值字符给字符串 (公开成员函数) | |
为字符串赋值 (公开成员函数) | |
构造 basic_string (公开成员函数) |