std::multimap<Key,T,Compare,Allocator>::insert

来自cppreference.com
< cpp‎ | container‎ | multimap
iterator insert( const value_type& value );
(1)
iterator insert( value_type&& value );
(1) (C++17 起)
template< class P >
iterator insert( P&& value );
(2) (C++11 起)
(3)
iterator insert( iterator pos, const value_type& value );
(C++11 前)
iterator insert( const_iterator hint, const value_type& value );
(C++11 起)
iterator insert( const_iterator hint, value_type&& value );
(3) (C++17 起)
template< class P >
iterator insert( const_iterator hint, P&& value );
(4) (C++11 起)
template< class InputIt >
void insert( InputIt first, InputIt last );
(5)
void insert( std::initializer_list<value_type> ilist );
(6) (C++11 起)
iterator insert( node_type&& nh );
(7) (C++17 起)
iterator insert( const_iterator hint, node_type&& nh );
(8) (C++17 起)

插入元素到容器中。

1-2) 插入 value。如果容器拥有带等价键的元素,那么插入到范围的上界。重载 (2) 等价于 emplace(std::forward<P>(value)),且只有在 std::is_constructible<value_type, P&&>::value == true 时才会参与重载决议。
3-4) 插入 value 到尽可能接近正好在 pos 之前的位置。重载 (4) 等价于 emplace_hint(hint, std::forward<P>(value)),且只有在 std::is_constructible<value_type, P&&>::value == true 时才会参与重载决议。
5) 插入来自范围 [first, last) 的元素。
6) 插入来自 initializer_list ilist 的元素。
7) 如果 nh 是空的结点把柄,那么什么都不做。否则插入 nh 所占有的元素到容器并返回指向被插入元素的迭代器。如果范围含有关键等价于存在于容器中的 nh.key() 的关键,那么在范围结尾插入元素。如果 nh 非空且 get_allocator() != nh.get_allocator(),那么行为未定义。
8) 如果 nh 是空的结点把柄,那么什么都不做并返回尾迭代器。否则,插入 nh 所占有的元素到容器,并返回指向拥有等于 nh.key() 的关键的元素的迭代器。元素被插入到尽可能接近正好在 pos 之前的位置。如果 nh 非空且 get_allocator() != nh.get_allocator(),那么行为未定义。

没有迭代器或引用会失效。如果插入成功,那么在结点把柄保有元素时获得的指向该元素的指针和引用会失效,而在提取前获得的指向元素的指针和引用变得有效。 (C++17 起)

参数

pos - 指向新元素将被插入位置之前的迭代器
value - 要插入的值
first, last - 要插入的元素范围
ilist - 插入值来源的 initializer_list
nh - 兼容的结点把柄
类型要求
-
InputIt 必须符合老式输入迭代器 (LegacyInputIterator) 的要求。

返回值

1-4) 指向被插入元素的迭代器。
5-6) (无)
7,8) 如果 nh 为空就是尾迭代器,否则是指向被插入元素的迭代器。

异常

1-4) 如果任何操作抛出异常,那么插入无效果。

复杂度

1-2) 与容器大小成对数,O(log(size()))
3-4) 如果插入恰好发生在正好在 pos 之前 的位置,那么是均摊常数,否则与容器大小成对数。
5-6) O(N*log(size() + N)),其中 N 是要插入的元素数。
7) 与容器大小成对数,O(log(size()))
8) 如果插入恰好发生在正好在 pos 之前 的位置,那么是均摊常数,否则与容器大小成对数。

示例

#include <iostream>
#include <string>
#include <map>
#include <functional>
 
template<class M>
void print(const std::string_view rem, const M& mmap)
{
    std::cout << rem << " ";
    for (const auto & e : mmap)
        std::cout << "{" << e.first << "," << e.second << "} ";
    std::cout << '\n';
}
 
int main()
{
    // 列表初始化
    std::multimap<int, std::string, std::greater<int>> mmap
        {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}};
    print("#1", mmap);
 
    // 用 value_type 插入
    mmap.insert(decltype(mmap)::value_type(5, "pqr"));
    print("#2", mmap);
 
    // 用 make_pair 插入
    mmap.insert(std::pair{6, "uvw"});
    print("#3", mmap);
 
    mmap.insert({7, "xyz"});
    print("#4", mmap);
 
    // 用 initialization_list 插入
    mmap.insert({{5, "one"}, {5, "two"}});
    print("#5", mmap);
 
    // 用一对迭代器插入
    mmap.clear();
    const auto il = {std::pair{1, "ä"}, {2, "ё"}, {2, "ö"}, {3, "ü"}};
    mmap.insert(il.begin(), il.end());
    print("#6", mmap);
}

输出:

#1 {5,def} {3,baz} {2,foo} {2,bar} {1,abc}
#2 {5,def} {5,pqr} {3,baz} {2,foo} {2,bar} {1,abc}
#3 {6,uvw} {5,def} {5,pqr} {3,baz} {2,foo} {2,bar} {1,abc}
#4 {7,xyz} {6,uvw} {5,def} {5,pqr} {3,baz} {2,foo} {2,bar} {1,abc}
#5 {7,xyz} {6,uvw} {5,def} {5,pqr} {5,one} {5,two} {3,baz} {2,foo} {2,bar} {1,abc}
#6 {3,ü} {2,ё} {2,ö} {1,ä}

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 出版时的行为 正确行为
LWG 233 C++98 pos 只是提示,可以完全忽略 必须在尽可能接近正好在
pos 之前的位置插入
LWG 264 C++98 重载 (5) 的复杂度在范围 [i, j) 已经按 Compare 排序的情况下要求是线性 取消这种情况下的线性复杂度要求

参阅

(C++11)
原位构造元素
(公开成员函数)
使用提示原位构造元素
(公开成员函数)