Namespaces
Variants

std::multimap<Key,T,Compare,Allocator>:: multimap

From cppreference.net

(1)
multimap ( ) ;
(C++11まで)
multimap ( ) : multimap ( Compare ( ) ) { }
(C++11から)
(constexprはC++26から)
explicit multimap ( const Compare & comp,
const Allocator & alloc = Allocator ( ) ) ;
(2) (C++26以降 constexpr)
explicit multimap ( const Allocator & alloc ) ;
(3) (C++11以降)
(C++26以降 constexpr)
template < class InputIt >

multimap ( InputIt first, InputIt last,
const Compare & comp = Compare ( ) ,

const Allocator & alloc = Allocator ( ) ) ;
(4) (constexpr since C++26)
template < class InputIt >

multimap ( InputIt first, InputIt last,
const Allocator & alloc )

: multimap ( first, last, Compare ( ) , alloc ) { }
(5) (C++14以降)
(C++26以降 constexpr)
multimap ( const multimap & other ) ;
(6) (C++26以降 constexpr)
multimap ( const multimap & other, const Allocator & alloc ) ;
(7) (C++11以降)
(C++26以降 constexpr)
multimap ( multimap && other ) ;
(8) (C++11以降)
(C++26以降 constexpr)
multimap ( multimap && other, const Allocator & alloc ) ;
(9) (C++11以降)
(C++26以降 constexpr)
multimap ( std:: initializer_list < value_type > init,

const Compare & comp = Compare ( ) ,

const Allocator & alloc = Allocator ( ) ) ;
(10) (C++11以降)
(C++26以降constexpr)
multimap ( std:: initializer_list < value_type > init,

const Allocator & alloc )

: multimap ( init, Compare ( ) , alloc ) { }
(11) (C++14以降)
(C++26以降 constexpr)
template < container-compatible-range < value_type > R >

multimap ( std:: from_range_t , R && rg,
const Compare & comp = Compare ( ) ,

const Allocator & alloc = Allocator ( ) ) ;
(12) (C++23以降)
(C++26以降constexpr)
template < container-compatible-range < value_type > R >

multimap ( std:: from_range_t , R && rg,
const Allocator & alloc )

: multimap ( std:: from_range , std:: forward < R > ( rg ) , Compare ( ) , alloc ) { }
(13) (C++23以降)
(C++26以降 constexpr)

様々なデータソースから新しいコンテナを構築し、オプションでユーザー提供のアロケータ alloc または比較関数オブジェクト comp を使用します。

1-3) 空のコンテナを構築します。
4,5) 範囲の内容でコンテナを構築します [ first , last )
[ first , last ) 有効な範囲 でない場合、動作は未定義です。
6,7) コンテナを other の内容のコピーで構築します。

alloc が提供されない場合、アロケータは std:: allocator_traits < allocator_type > ::
select_on_container_copy_construction ( other. get_allocator ( ) )
を呼び出すことで取得されます。

(C++11以降)

クラステンプレート引数推論 中、最初の引数のみがコンテナの Allocator テンプレートパラメータの推論に寄与します。

(C++23以降)
8,9) ムーブセマンティクスを使用して other の内容でコンテナを構築します。 alloc が提供されない場合、アロケータは other に属するアロケータからのムーブ構築によって取得されます。

クラステンプレート引数推論 の間、最初の引数のみがコンテナの Allocator テンプレートパラメータの推論に寄与します。

(C++23以降)
10,11) コンテナを初期化子リスト init の内容で構築します。
12,13) コンテナを rg の内容で構築します。

目次

パラメータ

alloc - このコンテナのすべてのメモリ割り当てに使用するアロケータ
comp - キーのすべての比較に使用する比較関数オブジェクト
first, last - コピーする要素のソース範囲を定義するイテレータのペア range
other - コンテナの要素を初期化するためのソースとして使用する別のコンテナ
init - コンテナの要素を初期化するための初期化子リスト
rg - container compatible range 、つまり要素が value_type に変換可能な input_range
型要件
-
InputIt LegacyInputIterator の要件を満たさなければならない
-
Compare Compare の要件を満たさなければならない
-
Allocator Allocator の要件を満たさなければならない

計算量

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 に対して線形時間となる。

例外

Allocator::allocate への呼び出しは例外をスローする可能性があります。

注記

コンテナのムーブ構築後(オーバーロード ( 8,9 ) )、 other への参照、ポインタ、およびイテレータ(終端イテレータを除く)は有効なままですが、現在は * this 内の要素を参照します。現在の標準では、この保証は [container.reqmts]/67 の包括的な記述によってなされており、より直接的な保証は LWG issue 2321 を通じて検討中です。

C++23で正式に要求される前から、一部の実装では既にテンプレートパラメータ Allocator 非推定コンテキスト に置いていた。

機能テスト マクロ 標準 機能
__cpp_lib_containers_ranges 202202L (C++23) Ranges-aware 構築と挿入; オーバーロード ( 12,13 )

#include <iostream>
#include <map>
#include <utility>
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
    }
};
template <typename Key, typename Value, typename Cmp>
void println(auto rem, const std::multimap<Key, Value, Cmp>& map)
{
    std::cout << rem << "{ ";
    for (auto n{map.size()}; const auto& p : map)
        std::cout << '[' << p.first << ":" << p.second << (--n ? "], " : "]");
    std::cout << " }\n";
}
int main()
{
    std::multimap<int, int> m1 =
    {
        {1, 1}, {2, 2}, {3, 3}, {4, 4}, {4, 4}, {3, 3}, {2, 2}, {1, 1}
    };
    println("m1 = ", m1);
    // カスタム比較関数
    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';
    std::cout << "Construction from a range:\n";
    using PS = std::pair<int, std::string>;
    const auto rg = {PS{3, "Earth"}, {2, "Venus"}, {1, "Mercury"}, {3, "Moon"}};
#if __cpp_lib_containers_ranges
    std::multimap<int, std::string> m2(std::from_range, rg); // オーバーロード (12)
#else
    std::multimap<int, std::string> m2(rg.begin(), rg.end()); // 代替案として (4)
#endif
    println("m2 = ", m2);
}

出力:

m1 = { [1:1], [1:1], [2:2], [2:2], [3:3], [3:3], [4:4], [4:4] }
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
Construction from a range:
m2 = { [1:Mercury], [2:Venus], [3:Earth], [3:Moon] }

欠陥報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 2076 C++11 オーバーロード ( 4 ) が条件付きで Key T CopyInsertable を要求 要求しない
LWG 2193 C++11 デフォルトコンストラクタがexplicit non-explicitに変更

関連項目

コンテナに値を代入する
(公開メンバ関数)