std::multimap<Key,T,Compare,Allocator>::multimap
提供: cppreference.com
multimap(); explicit multimap( const Compare& comp, {{#pad:|17}} const Allocator& alloc = Allocator() ); |
(1) | |
explicit multimap( const Allocator& alloc ); |
(1) | (C++11以上) |
| (2) | ||
template< class InputIt > multimap( InputIt first, InputIt last, {{#pad:|8}} const Compare& comp = Compare(), {{#pad:|8}} const Allocator& alloc = Allocator() ); |
||
template< class InputIt > multimap( InputIt first, InputIt last, {{#pad:|8}} const Allocator& alloc ); |
(C++14以上) | |
multimap( const multimap& other ); |
(3) | |
multimap( const multimap& other, const Allocator& alloc ); |
(3) | (C++11以上) |
multimap( multimap&& other ); |
(4) | (C++11以上) |
multimap( multimap&& other, const Allocator& alloc ); |
(4) | (C++11以上) |
| (5) | ||
multimap( std::initializer_list<value_type> init, {{#pad:|8}} const Compare& comp = Compare(), {{#pad:|8}} const Allocator& alloc = Allocator() ); |
(C++11以上) | |
multimap( std::initializer_list<value_type> init, {{#pad:|8}} const Allocator& ); |
(C++14以上) | |
オプションでユーザ提供のアロケータ alloc または比較関数 comp を使用して、様々なデータソースから新しいコンテナを構築します。
1) 空のコンテナを構築します。
2) 範囲
[first, last) の内容を持つコンテナを構築します。 3) コピーコンストラクタ。
other の内容のコピーを持つコンテナを構築します。 alloc が指定されない場合、アロケータは std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()) を呼ぶことによって取得されます。4) ムーブコンストラクタ。 ムーブセマンティクスを用いて
other の内容を持つコンテナを構築します。 alloc が指定されない場合、アロケータは other の持つアロケータからムーブ構築によって取得されます。5) 初期化子リスト
init の内容を持つコンテナを構築します。 引数
| alloc | - | このコンテナのすべてのメモリ確保に使用されるアロケータ |
| comp | - | キーのすべての比較に使用される比較関数 |
| first, last | - | 要素をコピーする範囲 |
| other | - | コンテナの要素を初期化するためのソースとして使用される別のコンテナ |
| init | - | コンテナの要素を初期化するための初期化子リスト |
| 型の要件 | ||
-InputIt は LegacyInputIterator の要件を満たさなければなりません。
| ||
-Compare は Compare の要件を満たさなければなりません。
| ||
-Allocator は Allocator の要件を満たさなければなりません。
|
計算量
1) 一定。
2) 一般的には N log(N)、ただし
N = std::distance(first, last)。 指定範囲が value_comp() によってすでにソートされている場合は N に比例。3)
other のサイズに比例。4) 一定。
alloc が指定されていて alloc != other.get_allocator() であれば比例。5) 一般的には N log(N)、ただし
N = init.size())。 指定範囲が value_comp() によってすでにソートされている場合は N に比例。例外
Allocator::allocate の呼び出しは例外を投げるかもしれません。
ノート
コンテナのムーブ構築 (オーバーロード (4)) の後、 other を指す参照、ポインタ、イテレータ (終端イテレータは除く) は有効なまま残りますが、以後 *this 内の要素を指すようになります。 現行の標準ではこの保証は [container.requirements.general]/12 の包括的な文言によってなされていますが、より直接的な保証が LWG 2321 で検討されています。
例
Run this code
#include <iostream>
#include <map>
struct Point { double x, y; };
struct PointCmp {
bool operator()(const Point& lhs, const Point& rhs) const {
return lhs.x < rhs.x; // NB. ignores y on purpose
}
};
int main() {
std::multimap<int, int> m = {{1,1},{2,2},{3,3},{4,4},{5,5},{4,4},{3,3},{2,2},{1,1}};
for(auto& p: m) std::cout << p.first << ' ' << p.second << '\n';
// custom comparison
std::multimap<Point, double, PointCmp> mag{
{ {5, 12}, 13 },
{ {3, 4}, 5 },
{ {8, 15}, 17 },
{ {3, -3}, -1 },
};
for(auto p : mag)
std::cout << "The magnitude of (" << p.first.x
<< ", " << p.first.y << ") is "
<< p.second << '\n';
}
出力:
1 1
1 1
2 2
2 2
3 3
3 3
4 4
4 4
5 5
The magnitude of (3, 4) is 5
The magnitude of (3, -3) is -1
The magnitude of (5, 12) is 13
The magnitude of (8, 15) is 17
欠陥報告
以下の動作変更欠陥報告は以前に発行された C++ 標準に遡って適用されました。
| DR | 適用先 | 発行時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 2193 | C++11 | the default constructor is explicit | made non-explicit |
関連項目
| コンテナに値を代入します (パブリックメンバ関数) |