共用方式為


multimap::multimap

建構一個空的多重對應 (Multimap) ,或者建構為其他多重對應的全部、部份複製。

multimap( );
explicit multimap(
    const Traits& Comp
);
multimap(
    const Traits& Comp,
    const Allocator& Al
);
map(
    const multimap& Right
);
multimap(
    multimap&& Right
);
multimap(
    initializer_list<value_type> IList
);
multimap(
    initializer_list<value_type> IList,
    const Compare& Comp
);
multimap(
    initializer_list<value_type> IList,
    const Compare& Comp, 
    const Allocator& Al
);
template<class InputIterator>
    multimap(
        InputIterator First,
        InputIterator Last
    );
template<class InputIterator>
    multimap(
        InputIterator First,
        InputIterator Last,
        const Traits& Comp
    );
template<class InputIterator>
    multimap(
        InputIterator First,
        InputIterator Last,
        const Traits& Comp,
    const Allocator& Al
    );

參數

參數

說明

Al

用於多重對應物件的儲存配置器類別,預設為 Allocator。

Comp

const Traits 型別的比較函式用於排序對應 (Map) 的元素,預設為 Traits

Right

在對應上,其建構的集合會成為複本。

First

在要複製之項目範圍中第一個項目的位置。

Last

超出要複製之項目範圍的第一個項目的位置。

IList

initializer_list 用於複製元素。

備註

建構函式會儲存一種配置器物件型別,以用於多重對應管理記憶體儲存,並可藉由呼叫 get_allocator 後傳回此物件。 配置器參數通常會在類別宣告中被省略,而前置處理巨集器會以替代配置器來做代換。

所有的建構函式都初始化其多重對應。

建構函式會儲存一種 Traits 型別的函式物件,以用於在多重對應索引值中建立順序,並可藉由呼叫 key_comp 後傳回此物件。

前三個建構函式表示此為一個初始化為空的多重對應,第二個建構函式則指定比較函式的型別 (Comp) ,用於建立元素順序,第三個則明確說明用到的配置器型別 (Al) 。 關鍵字 explicit 禁止某些型別的自動轉換。

第四個建構函式指定多重對應 Right的複本。

第五個建構函式藉由移動 Right 以指定多重對應的複本。

第七、八個建構函式複製 initializer_list 的成員。

接下來的三個建構函式會在比較函式類別 Traits 型別和配置器中,複製對應範圍 [First, Last) 。

範例

// multimap_ctor.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main()
{
    using namespace std;
    typedef pair <int, int> Int_Pair;

    // Create an empty multimap m0 of key type integer
    multimap <int, int> m0;

    // Create an empty multimap m1 with the key comparison
    // function of less than, then insert 4 elements
    multimap <int, int, less<int> > m1;
    m1.insert(Int_Pair(1, 10));
    m1.insert(Int_Pair(2, 20));
    m1.insert(Int_Pair(3, 30));
    m1.insert(Int_Pair(4, 40));

    // Create an empty multimap m2 with the key comparison
    // function of geater than, then insert 2 elements
    multimap <int, int, less<int> > m2;
    m2.insert(Int_Pair(1, 10));
    m2.insert(Int_Pair(2, 20));

    // Create a multimap m3 with the 
    // allocator of multimap m1
    multimap <int, int>::allocator_type m1_Alloc;
    m1_Alloc = m1.get_allocator();
    multimap <int, int> m3(less<int>(), m1_Alloc);
    m3.insert(Int_Pair(3, 30));

    // Create a copy, multimap m4, of multimap m1
    multimap <int, int> m4(m1);

    // Create a multimap m5 by copying the range m1[_First, _Last)
    multimap <int, int>::const_iterator m1_bcIter, m1_ecIter;
    m1_bcIter = m1.begin();
    m1_ecIter = m1.begin();
    m1_ecIter++;
    m1_ecIter++;
    multimap <int, int> m5(m1_bcIter, m1_ecIter);

    // Create a multimap m6 by copying the range m4[_First, _Last)
    // and with the allocator of multimap m2
    multimap <int, int>::allocator_type m2_Alloc;
    m2_Alloc = m2.get_allocator();
    multimap <int, int> m6(m4.begin(), ++m4.begin(), less<int>(), m2_Alloc);

    cout << "m1 =";
    for (auto i : m1)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    cout << "m2 =";
    for (auto i : m2)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    cout << "m3 =";
    for (auto i : m3)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    cout << "m4 =";
    for (auto i : m4)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    cout << "m5 =";
    for (auto i : m5)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    cout << "m6 =";
    for (auto i : m6)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    // Create a multimap m8 by copying in an initializer_list
    multimap<int, int> m8{ { { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 } } };
    cout << "m8: = ";
    for (auto i : m8)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    // Create a multimap m9 with an initializer_list and a comparator
    multimap<int, int> m9({ { 5, 5 }, { 6, 6 }, { 7, 7 }, { 8, 8 } }, less<int>());
    cout << "m9: = ";
    for (auto i : m9)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

    // Create a multimap m10 with an initializer_list, a comparator, and an allocator
    multimap<int, int> m10({ { 9, 9 }, { 10, 10 }, { 11, 11 }, { 12, 12 } }, less<int>(), m9.get_allocator());
    cout << "m10: = ";
    for (auto i : m10)
        cout << i.first << " " << i.second << ", ";
    cout << endl;

}

Output

  

需求

標頭:<map>

命名空間: std

請參閱

參考

multimap 類別

標準樣板程式庫