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以降) |
内容パラメータ
計算量
1-3)
定数。
4,5)
N·log(N)
ここで
N
は
std::
distance
(
first, last
)
である。一般的には
N
に対して線形時間だが、
[
first
,
last
)
が既に
value_comp
(
)
によってソートされている場合は
N
に対して線形時間となる。
6,7)
サイズに対して線形
other
。
8,9)
定数。もし
alloc
が与えられ、かつ
alloc
!
=
other.
get_allocator
(
)
の場合、線形。
10,11)
N·log(N)
ここで
N
は
init.
size
(
)
一般には
N
に対して線形、
init
が既に
value_comp
(
)
でソートされている場合は
N
に対して線形。
12,13)
N·log(N)
ここで
N
は
ranges::
distance
(
rg
)
です。一般的には
N
に対して線形時間ですが、
rg
が既に
value_comp
(
)
でソートされている場合は
N
に対して線形時間となります。
例外
注記コンテナのムーブ構築(オーバーロード (8,9))の後、元々 C++23までは正式には要求されていませんが、一部の実装ではすでにテンプレートパラメータ
例
このコードを実行
#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++規格に対して遡及的に適用されました。
関連項目
| ||||||||||||||||||||||||||||||||||||||||||||||||||||