名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | string‎ | basic_string
 
 
 
std::basic_string
 
template< container-compatible-range<CharT> R >
constexpr iterator insert_range( const_iterator pos, R&& rg );
(C++23 起)

pos 指向的元素(如果存在)之前插入來自範圍 rg 的字元。

等價於

return insert(pos - begin(),
    std::basic_string(
        std::from_range,
        std​::​forward<R>(rg),
        get_allocator())
);

如果 pos*this 上不是有效的迭代器,則行為未定義。

目錄

[編輯] 引數

pos - 字元將插入其之前的迭代器
rg - 一個容器兼容範圍

[編輯] 返回值

一個指向第一個插入字元的迭代器,如果由於 rg 為空而未插入字元,則為 pos

[編輯] 複雜度

rg 的大小呈線性關係。

[編輯] 異常

如果 std::allocator_traits<Allocator>::allocate 丟擲異常,則重新丟擲。

如果操作將導致 size() 超出 max_size(),則丟擲 std::length_error

如果由於任何原因丟擲異常,此函式無效果(強異常安全保證)。

[編輯] 注意

特性測試 標準 特性
__cpp_lib_containers_ranges 202202L (C++23) 接受容器兼容範圍的成員函式

[編輯] 示例

#include <cassert>
#include <iterator>
#include <string>
 
int main()
{
    const auto source = {'l', 'i', 'b', '_'};
    std::string target{"__cpp_containers_ranges"};
    //                        ^insertion will occur
    //                         before this position
 
    const auto pos = target.find("container");
    assert(pos != target.npos);
    auto iter = std::next(target.begin(), pos);
 
#ifdef __cpp_lib_containers_ranges
    target.insert_range(iter, source);
#else
    target.insert(iter, source.begin(), source.end());
#endif
 
    assert(target == "__cpp_lib_containers_ranges");
    //                      ^^^^
}

[編輯] 參閱

插入字元
(public member function) [編輯]