定义于头文件 <map>
template< class Key, | (1) | |
namespace pmr { template <class Key, class T, class Compare = std::less<Key>> | (2) | (C++17 起) |
multimap 是关联容器,含有关键-值 pair 的已排序列表,同时容许多个入口拥有同一关键。按照应用到关键的比较函数 Compare
排序。搜索、插入和移除操作拥有对数复杂度。
拥有等价关键的关键-值 pair 的顺序就是插入顺序,且不会更改。(C++11 起)
凡在标准库使用比较 (Compare) 概念出,都用描述于比较 (Compare) 上的等价关系确定等价性。不精确地说,若二个对象 a
和 b
互不小于对方: !comp(a, b) && !comp(b, a)
,则认为它们等价。
修改器
插入元素或结点
std::multimap<Key,T,Compare,Allocator>::insert
iterator insert( const value_type& value ); | (1) | |
iterator insert( value_type&& value ); | (1) | (C++17 起) |
template< class P > | (2) | (C++11 起) |
iterator insert( iterator hint, const value_type& value ); | (3) | (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 > | (4) | (C++11 起) |
template< class InputIt > | (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
。若容器拥有带等价关键的元素,则插入到范围的上界。(C++11 起)重载 (2) 等价于 emplace(std::forward<P>(value)) ,且仅若 std::is_constructible<value_type, P&&>::value == true 才参与重载决议。
3-4) 插入 value
到尽可能接近,恰好前于(C++11 起) hint
的位置。重载 (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() 的关键的元素的迭代器元素被插入到尽可能接近正好先于 hint
的位置。若 nh
非空且 get_allocator() != nh.get_allocator() 则行为未定义。
没有迭代器或引用被非法化。若插入成功,则在结点把柄保有元素时获得的指向该元素的指针和引用被非法化,而在提取前获得的指向元素的指针和引用变得合法。 (C++17 起)
参数
hint | - |
| ||||
value | - | 要插入的值 | ||||
first, last | - | 要插入的元素范围 | ||||
ilist | - | 插入值来源的 initializer_list | ||||
nh | - | 兼容的结点把柄 | ||||
类型要求 | ||||||
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。 |
返回值
1-4) 指向被插入元素的迭代器。
5-6) (无)
7,8) 若 nh
为则为尾迭代器,否则为指向被插入元素的迭代器。
异常
1-4) 若任何操作抛出异常,则插入无效果。
本节未完成 原因:情况 5-6 |
复杂度
1-2) 与容器大小成对数, O(log(size()))
。
3-4) 若插入恰好发生在 hint 后的位置则为均摊常数,否则与容器大小成对数。 | (C++11 前) |
3-4) 若插入恰好发生在 hint 前的位置则为均摊常数,否则与容器大小成对数。 | (C++11 起) |
5-6) O(N*log(size() + N))
,其中 N 是要插入的元素数。
7) 与容器大小成对数, O(log(size()))
。
8) 若插入恰好发生在 hint 前的位置则为均摊常数,否则与容器大小成对数。
调用示例
#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <map>
#include <time.h>
using namespace std;
struct Cell
{
int x;
int y;
Cell() = default;
Cell(int a, int b): x(a), y(b) {}
Cell &operator +=(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator +(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator *(const Cell &cell)
{
x *= cell.x;
y *= cell.y;
return *this;
}
Cell &operator ++()
{
x += 1;
y += 1;
return *this;
}
bool operator <(const Cell &cell) const
{
if (x == cell.x)
{
return y < cell.y;
}
else
{
return x < cell.x;
}
}
bool operator >(const Cell &cell) const
{
if (x == cell.x)
{
return y > cell.y;
}
else
{
return x > cell.x;
}
}
bool operator ==(const Cell &cell) const
{
return x == cell.x && y == cell.y;
}
};
struct myCompare
{
bool operator()(const int &a, const int &b)
{
return a < b;
}
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
os << "{" << cell.x << "," << cell.y << "}";
return os;
}
std::ostream &operator<<(std::ostream &os, const std::pair<const int, Cell> &pCell)
{
os << pCell.first << "-" << pCell.second;
return os;
}
int main()
{
std::cout << std::boolalpha;
std::mt19937 g{std::random_device{}()};
srand((unsigned)time(NULL));
auto genKey = []()
{
return std::rand() % 10 + 100;
};
auto generate = []()
{
int n = std::rand() % 10 + 100;
Cell cell{n, n};
return cell;
};
std::multimap<int, Cell> multimap1;
for (size_t index = 0; index < 5; index++)
{
//1-2) 插入 value 。若容器拥有带等价关键的元素。
multimap1.insert({genKey(), generate()});
std::cout << "multimap1: ";
std::copy(multimap1.begin(), multimap1.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
std::cout << std::endl;
}
std::cout << std::endl;
std::multimap<int, Cell> multimap2;
for (size_t index = 0; index < 5; index++)
{
//1-2) 插入 value 。若容器拥有带等价关键的元素。移动语义
std::pair<int, Cell> tpair{genKey(), generate()};
multimap2.insert(std::move(tpair));
std::cout << "multimap2: ";
std::copy(multimap2.begin(), multimap2.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
std::cout << std::endl;
}
std::cout << std::endl;
std::multimap<int, Cell> multimap3;
for (size_t index = 0; index < 5; index++)
{
//3-4) 插入 value 到尽可能接近,恰好前于(C++11 起) hint 的位置。移动语义
multimap3.insert(multimap3.begin(), {genKey(), generate()});
std::cout << "multimap3: ";
std::copy(multimap3.begin(), multimap3.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
std::cout << std::endl;
}
std::cout << std::endl;
std::multimap<int, Cell> multimap4;
for (size_t index = 0; index < 5; index++)
{
//3-4) 插入 value 到尽可能接近,恰好前于(C++11 起) hint 的位置。移动语义
std::pair<int, Cell> tpair{genKey(), generate()};
multimap4.insert(multimap4.begin(), std::move(tpair));
std::cout << "multimap4: ";
std::copy(multimap4.begin(), multimap4.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
std::cout << std::endl;
}
std::cout << std::endl;
std::multimap<int, Cell> multimap5;
for (size_t index = 0; index < 5; index++)
{
//5) 插入来自范围 [first, last) 的元素。
multimap5.insert(multimap3.begin(), multimap3.end());
std::cout << "multimap5: ";
std::copy(multimap5.begin(), multimap5.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
std::cout << std::endl;
}
std::cout << std::endl;
std::multimap<int, Cell> multimap6;
//6) 插入来自 initializer_list ilist 的元素。
multimap6.insert({{genKey(), generate()}, {genKey(), generate()},
{genKey(), generate()}, {genKey(), generate()}, {genKey(), generate()}});
std::cout << "multimap6: ";
std::copy(multimap6.begin(), multimap6.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
std::cout << std::endl;
return 0;
}