std::multimap<Key,T,Compare,Allocator>::insert
来自 cppreference.cn
                    
                                        
                    
                    
                                                            
                    | iterator insert( const value_type& value ); | (1) | |
| iterator insert( value_type&& value ); | (2) | (C++17 起) | 
| template< class P > iterator insert( P&& value ); | (3) | (C++11 起) | 
| (4) | ||
| iterator insert( iterator pos, const value_type& value ); | (C++11 前) | |
| iterator insert( const_iterator pos, const value_type& value ); | (C++11 起) | |
| iterator insert( const_iterator pos, value_type&& value ); | (5) | (C++17 起) | 
| template< class P > iterator insert( const_iterator pos, P&& value ); | (6) | (C++11 起) | 
| template< class InputIt > void insert( InputIt first, InputIt last ); | (7) | |
| void insert( std::initializer_list<value_type> ilist ); | (8) | (C++11 起) | 
| iterator insert( node_type&& nh ); | (9) | (C++17 起) | 
| iterator insert( const_iterator pos, node_type&& nh ); | (10) | (C++17 起) | 
将元素插入容器中。
1-3) 插入 value。如果容器中存在键等价的元素,则在对应范围的上界处插入。
 重载 (3) 等价于 emplace(std::forward<P>(value)),且仅当 std::is_constructible<value_type, P&&>::value == true 时参与重载决议。
4-6) 在尽可能接近 pos 之前的插入点插入 value。
 重载 (6) 等价于 emplace_hint(hint, std::forward<P>(value)),且仅当 std::is_constructible<value_type, P&&>::value == true 时参与重载决议。
7) 插入范围 
[first, last) 中的元素。8) 插入初始化器列表 ilist 中的元素。
9) 若 nh 为空节点句柄,则不执行任何操作。否则,将 nh 所拥有的元素插入容器并返回指向插入元素的迭代器。若容器中存在键等价于 nh.key() 的元素范围,则在范围末尾插入元素。若 nh 不为空且 get_allocator() != nh.get_allocator(),则行为未定义。
10) 若 nh 为空节点句柄,则不执行任何操作并返回末尾迭代器。否则,将 nh 所拥有的元素插入容器,并返回指向键等价于 nh.key() 的元素的迭代器。元素将插入在尽可能接近 pos 之前的插入点。若 nh 不为空且 get_allocator() != nh.get_allocator(),则行为未定义。
没有迭代器或引用被失效。如果插入成功,则在节点句柄中持有时获得的指向元素的指针和引用失效,而在元素被提取前获得的指向该元素的指针和引用变为有效。(C++17 起)
| 目录 | 
[编辑] 参数
| pos | - | 指向新元素将插入位置之前的迭代器 | 
| value | - | 要插入的元素值 | 
| first, last | - | 定义要插入的元素源范围的迭代器对 | 
| ilist | - | 要从中插入值的初始化列表 | 
| nh | - | 兼容的节点句柄 | 
| 类型要求 | ||
| - InputIt必须满足 LegacyInputIterator 的要求。 | ||
[编辑] 返回值
1-6) 指向已插入元素的迭代器。
7,8) (无)
9,10) 若 nh 为空,则为末尾迭代器;否则为指向插入元素的迭代器。
[编辑] 异常
1-6) 如果任何操作抛出异常,则插入无效。
7,8) 无异常安全保证。
9,10) 如果任何操作抛出异常,插入操作没有效果。
[编辑] 复杂度
1-3) 
O(log(size()))4-6) 如果插入发生在 pos 之前的插入点,则为均摊常数复杂度;否则为 
O(log(size()))。7,8) 
O(N·log(size() + N)),其中 N 是要插入的元素数量。9) 
O(log(size()))10) 如果插入发生在 pos 之前的插入点,则为均摊常数复杂度;否则为 
O(log(size()))。[编辑] 示例
运行此代码
#include <functional> #include <iostream> #include <map> #include <string> #include <string_view> #include <utility> 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() { // list-initialize std::multimap<int, std::string, std::greater<int>> mmap {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}}; print("#1", mmap); // insert using value_type mmap.insert(decltype(mmap)::value_type(5, "pqr")); print("#2", mmap); // insert using pair mmap.insert(std::pair{6, "uvw"}); print("#3", mmap); mmap.insert({7, "xyz"}); print("#4", mmap); // insert using initializer_list mmap.insert({{5, "one"}, {5, "two"}}); print("#5", mmap); // insert using a pair of iterators 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 | 如果范围 [first, last)根据Compare排序,则重载 (5) 的复杂度要求为线性范围 [first, last)依据Compare排序 | 在此特殊情况下 取消线性要求 | 
| LWG 371 | C++98 | 等价元素的顺序 未保证保持 | 必须保留 | 
| LWG 2005 | C++11 | 重载 (3,6) 描述不佳 | 改进了描述 | 
[编辑] 亦参见
| (C++11) | 就地构造元素 (public member function) | 
| (C++11) | 使用提示就地构造元素 (public member function) | 
| 创建从参数推断类型的std::insert_iterator (function template) | 


