std::map<Key,T,Compare,Allocator>::map
提供: cppreference.com
map(); explicit map( const Compare& comp, {{#pad:|12}} const Allocator& alloc = Allocator() ); |
(1) | |
explicit map( const Allocator& alloc ); |
(1) | (C++11以上) |
| (2) | ||
template< class InputIt > map( InputIt first, InputIt last, {{#pad:|3}} const Compare& comp = Compare(), {{#pad:|3}} const Allocator& alloc = Allocator() ); |
||
template< class InputIt > map( InputIt first, InputIt last, {{#pad:|3}} const Allocator& alloc ); |
(C++14以上) | |
map( const map& other ); |
(3) | |
map( const map& other, const Allocator& alloc ); |
(3) | (C++11以上) |
map( map&& other ); |
(4) | (C++11以上) |
map( map&& other, const Allocator& alloc ); |
(4) | (C++11以上) |
| (5) | ||
map( std::initializer_list<value_type> init, {{#pad:|3}} const Compare& comp = Compare(), {{#pad:|3}} const Allocator& alloc = Allocator() ); |
(C++11以上) | |
map( std::initializer_list<value_type> init, {{#pad:|3}} const Allocator& ); |
(C++14以上) | |
オプションでユーザ提供のアロケータ alloc または比較関数 comp を使用して、様々なデータソースから新しいコンテナを構築します。
1) 空のコンテナを構築します。
3) コピーコンストラクタ。
other の内容のコピーを持つコンテナを構築します。 alloc が指定されない場合、アロケータは std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()) を呼ぶことによって取得されます。4) ムーブコンストラクタ。 ムーブセマンティクスを用いて
other の内容を持つコンテナを構築します。 alloc が指定されない場合、アロケータは other の持つアロケータからムーブ構築によって取得されます。引数
| 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 <string>
#include <iomanip>
#include <map>
template<typename Map>
void print_map(Map& m)
{
std::cout << '{';
for(auto& p: m)
std::cout << p.first << ':' << p.second << ' ';
std::cout << "}\n";
}
struct Point { double x, y; };
struct PointCmp {
bool operator()(const Point& lhs, const Point& rhs) const {
return lhs.x < rhs.x; // NB. intentionally ignores y
}
};
int main()
{
// (1) Default constructor
std::map<std::string, int> map1;
map1["something"] = 69;
map1["anything"] = 199;
map1["that thing"] = 50;
std::cout << "map1 = "; print_map(map1);
// (2) Range constructor
std::map<std::string, int> iter(map1.find("anything"), map1.end());
std::cout << "\niter = "; print_map(iter);
std::cout << "map1 = "; print_map(map1);
// (3) Copy constructor
std::map<std::string, int> copied(map1);
std::cout << "\ncopied = "; print_map(copied);
std::cout << "map1 = "; print_map(map1);
// (4) Move constructor
std::map<std::string, int> moved(std::move(map1));
std::cout << "\nmoved = "; print_map(moved);
std::cout << "map1 = "; print_map(map1);
// (5) Initializer list constructor
const std::map<std::string, int> init {
{"this", 100},
{"can", 100},
{"be", 100},
{"const", 100},
};
std::cout << "\ninit = "; print_map(init);
// Custom Key class option 1:
// Use a comparison struct
std::map<Point, double, PointCmp> mag = {
{ {5, -12}, 13 },
{ {3, 4}, 5 },
{ {-8, -15}, 17 }
};
for(auto p : mag)
std::cout << "The magnitude of (" << p.first.x
<< ", " << p.first.y << ") is "
<< p.second << '\n';
// Custom Key class option 2:
// Use a comparison lambda
// This lambda sorts points according to their magnitudes, where note that
// these magnitudes are taken from the local variable mag
auto cmpLambda = [&mag](const Point &lhs, const Point &rhs) { return mag[lhs] < mag[rhs]; };
//You could also use a lambda that is not dependent on local variables, like this:
//auto cmpLambda = [](const Point &lhs, const Point &rhs) { return lhs.y < rhs.y; };
std::map<Point, double, decltype(cmpLambda)> magy(cmpLambda);
//Various ways of inserting elements:
magy.insert(std::pair<Point, double>({5, -12}, 13));
magy.insert({ {3, 4}, 5});
magy.insert({Point{-8.0, -15.0}, 17});
std::cout << '\n';
for(auto p : magy)
std::cout << "The magnitude of (" << p.first.x
<< ", " << p.first.y << ") is "
<< p.second << '\n';
}
出力:
map1 = {anything:199 something:69 that thing:50 }
iter = {anything:199 something:69 that thing:50 }
map1 = {anything:199 something:69 that thing:50 }
copied = {anything:199 something:69 that thing:50 }
map1 = {anything:199 something:69 that thing:50 }
moved = {anything:199 something:69 that thing:50 }
map1 = {}
init = {be:100 can:100 const:100 this:100 }
The magnitude of (-8, -15) is 17
The magnitude of (3, 4) is 5
The magnitude of (5, -12) is 13
The magnitude of (3, 4) is 5
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 |
関連項目
| コンテナに値を代入します (パブリックメンバ関数) |