std::map<Key,T,Compare,Allocator>:: map
| (1) | ||
|
map
(
)
;
|
(C++11まで) | |
|
map
(
)
:
map
(
Compare
(
)
)
{
}
|
(C++11から)
(constexprはC++26から) |
|
|
explicit
map
(
const
Compare
&
comp,
const Allocator & alloc = Allocator ( ) ) ; |
(2) | (constexpr since C++26) |
|
explicit
map
(
const
Allocator
&
alloc
)
;
|
(3) |
(C++11以降)
(C++26以降 constexpr) |
|
template
<
class
InputIt
>
map
(
InputIt first, InputIt last,
|
(4) | (C++26以降 constexpr) |
|
template
<
class
InputIt
>
map
(
InputIt first, InputIt last,
|
(5) |
(C++14以降)
(C++26以降 constexpr) |
|
map
(
const
map
&
other
)
;
|
(6) | (C++26以降 constexpr) |
|
map
(
const
map
&
other,
const
Allocator
&
alloc
)
;
|
(7) |
(C++11以降)
(C++26以降 constexpr) |
|
map
(
map
&&
other
)
;
|
(8) |
(C++11以降)
(C++26以降 constexpr) |
|
map
(
map
&&
other,
const
Allocator
&
alloc
)
;
|
(9) |
(C++11以降)
(C++26以降constexpr) |
|
map
(
std::
initializer_list
<
value_type
>
init,
const
Compare
&
comp
=
Compare
(
)
,
|
(10) |
(C++11以降)
(C++26以降 constexpr) |
|
map
(
std::
initializer_list
<
value_type
>
init,
const
Allocator
&
alloc
)
|
(11) |
(C++14以降)
(C++26以降constexpr) |
|
template
<
container-compatible-range
<
value_type
>
R
>
map
(
std::
from_range_t
, R
&&
rg,
|
(12) |
(C++23以降)
(C++26以降constexpr) |
|
template
<
container-compatible-range
<
value_type
>
R
>
map
(
std::
from_range_t
, R
&&
rg,
|
(13) |
(C++23以降)
(C++26以降 constexpr) |
様々なデータソースから新しいコンテナを構築し、オプションでユーザー提供のアロケータ alloc または比較関数オブジェクト comp を使用します。
[
first
,
last
)
。
|
alloc
が提供されない場合、アロケータは
std::
allocator_traits
<
allocator_type
>
::
|
(C++11以降) |
|
クラステンプレート引数推論
中、最初の引数のみがコンテナの
|
(C++23以降) |
|
クラステンプレート引数推論
の間、最初の引数のみがコンテナの
|
(C++23以降) |
目次 |
パラメータ
| alloc | - | このコンテナのすべてのメモリ割り当てに使用するアロケータ |
| comp | - | キーのすべての比較に使用する比較関数オブジェクト |
| first, last | - | コピーする要素のソース範囲を定義するイテレータのペア range |
| other | - | コンテナの要素を初期化するためのソースとして使用する別のコンテナ |
| init | - | コンテナの要素を初期化するための初期化子リスト |
| rg | - |
コンテナ互換範囲
、つまり要素が
value_type
に変換可能な
input_range
|
| 型要件 | ||
-
InputIt
は
LegacyInputIterator
の要件を満たさなければならない
|
||
-
Compare
は
Compare
の要件を満たさなければならない
|
||
-
Allocator
は
Allocator
の要件を満たさなければならない
|
||
計算量
[
first
,
last
)
が既に
value_comp
(
)
によってソートされている場合は
N
に対して線形時間となる。
例外
Allocator::allocate
への呼び出しは例外をスローする可能性があります。
注記
コンテナのムーブ構築後(オーバーロード ( 8,9 ) )、 other への参照、ポインタ、およびイテレータ(終端イテレータを除く)は有効なままですが、現在は * this 内の要素を参照します。現在の標準はこの保証を [container.reqmts]/67 の包括的な記述を通じて行っており、より直接的な保証が LWG issue 2321 を通じて検討中です。
範囲内に比較等価なキーを持つ複数の要素がある場合、どの要素が挿入されるかは未規定です( LWG2844 未解決)。
C++23で正式に要求される前から、一部の実装では既にテンプレートパラメータ
Allocator
を
non-deduced contexts
に含めているものがあります。
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_containers_ranges
|
202202L
|
(C++23) | Ranges-aware 構築と挿入; オーバーロード ( 12,13 ) |
例
#include <iomanip> #include <iostream> #include <map> #include <string> template<typename Key, typename Value, typename Cmp> std::ostream& operator<<(std::ostream& os, const std::map<Key, Value, Cmp>& map) { os << "{ "; for (auto comma{map.size()}; const auto& p : map) os << '\'' << p.first << "'は " << p.second << (--comma ? ", " : " "); return os << "}\n"; } struct Point { double x, y; friend std::ostream& operator<<(std::ostream& os, Point pt) { return os << '(' << pt.x << ", " << pt.y << ')'; } }; struct PointCmp { bool operator()(const Point& lhs, const Point& rhs) const { return lhs.x < rhs.x; // NB: y は意図的に無視されます } }; int main() { // (1) デフォルトコンストラクタ std::map<std::string, int> map1; map1["something"] = 69; map1["anything"] = 199; map1["あのもの"] = 50; std::cout << "map1 = " << map1; // (4) 範囲コンストラクタ std::map<std::string, int> iter(map1.find("anything"), map1.end()); std::cout << "\niter = " << iter; std::cout << "map1 = " << map1; // (6) コピーコンストラクタ std::map<std::string, int> copied(map1); std::cout << "\ncopied = " << copied; std::cout << "map1 = " << map1; // (8) ムーブコンストラクタ std::map<std::string, int> moved{std::move(map1)}; std::cout << "\nmoved = " << moved; std::cout << "map1 = " << map1; // (10) 初期化子リストコンストラクタ const std::map<std::string, int> init { {"this", 100}, {"can", 100}, {"be", 100}, {"const", 100} }; std::cout << "\ninit = " << init; std::cout << "\nカスタムキークラス オプション 1:\n"; // 比較用の構造体を使用 std::map<Point, double, PointCmp> mag = { {{5, -12}, 13}, {{3, 4}, 5}, {{-8, -15}, 17} }; std::cout << "mag = " << mag << '\n'; std::cout << "カスタムキークラス オプション 2:\n"; // 比較ラムダを使用 // このラムダは点をその大きさに従ってソートします。ここで // これらの大きさはローカル変数 mag から取得されます。 auto cmpLambda = [&mag](const Point& lhs, const Point& rhs) { return mag[lhs] < mag[rhs]; }; // ローカル変数に依存しないラムダを使用することもできます。このように: // auto cmpLambda = [](const Point& lhs, const Point& rhs){ return lhs.y < rhs.y; }; std::map<Point, double, decltype(cmpLambda)> magy(cmpLambda); // 要素を挿入する様々な方法: magy.insert(std::pair<Point, double>({5, -12}, 13)); magy.insert({{3, 4}, 5}); magy.insert({Point{-8.0, -15.0}, 17}); std::cout << "magy = " << magy << '\n'; std::cout << "範囲からの構築:\n"; using PS = std::pair<const std::string, int>; const auto rg = {PS{"one", 1}, {"one", 101}, {"two", 2}, {"three", 3}}; #if __cpp_lib_containers_ranges std::map<std::string, int> nums(std::from_range, rg); // オーバーロード (12) #else std::map<std::string, int> nums(rg.begin(), rg.end()); // (4) へのフォールバック #endif std::cout << "nums = " << nums << '\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 }
カスタムキークラス オプション1:
mag = { '(-8, -15)' は 17, '(3, 4)' は 5, '(5, -12)' は 13 }
カスタムキークラス オプション2:
magy = { '(3, 4)' は 5, '(5, -12)' は 13, '(-8, -15)' は 17 }
範囲からの構築:
nums = { 'one' は 1, 'three' は 3, 'two' は 2 }
欠陥報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 2076 | C++11 |
オーバーロード
(
4
)
が条件付きで
Key
と
T
の
CopyInsertable
を要求
*
this
|
要求しない |
| LWG 2193 | C++11 | デフォルトコンストラクタがexplicit | non-explicitに変更 |
関連項目
|
コンテナに値を代入する
(公開メンバ関数) |