命名空间
变体
操作

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

来自 cppreference.cn
< 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) [编辑]