名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | string‎ | basic_string
 
 
 
std::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) [編輯]