multiset::multiset
建構一個空的多重對應 (Multimap) ,或者建構為其他多重對應的全部、部份複製。
multiset( );
explicit multiset (
const Compare& Comp
);
multiset (
const Compare& Comp,
const Allocator& Al
);
multiset(
const multiset& Right
);
multiset(
multiset&& Right
);
multiset(
initializer_list<Type> IList
);
multiset(
initializer_list<Type> IList,
const Compare& Comp
);
multiset(
initializer_list<Type> IList,
const Compare& Comp,
const Allocator& Al
);
template<class InputIterator>
multiset (
InputIterator First,
InputIterator Last
);
template<class InputIterator>
multiset (
InputIterator First,
InputIterator Last,
const Compare& Comp
);
template<class InputIterator>
multiset (
InputIterator First,
InputIterator Last,
const Compare& Comp,
const Allocator& Al
);
參數
參數 |
說明 |
Al |
這個多重設定物件所使用的儲存體配置器類別,預設值為 Allocator 。 |
Comp |
用於排序多重集合元素的 const Compare 型別比較函式,其預設值為 Compare。 |
Right |
建構的多重集是複製的多重集。 |
First |
在要複製之項目範圍中第一個項目的位置。 |
Last |
超出要複製之項目範圍的第一個項目的位置。 |
IList |
initializer_list 用於複製元素。 |
備註
建構函式會儲存一種配置器物件型別,以用於多重集合管理記憶體儲存,並可藉由呼叫 get_allocator 後傳回此物件。 配置器參數通常會在類別宣告中被省略,而前置處理巨集器會以替代配置器來做代換。
所有的建構函式都初始化其多重集合。
建構函式會儲存一種 Compare 型別的函式物件,以用於在多重集合索引值中建立順序,並可藉由呼叫key_comp 後傳回此物件。
前三個建構函式表示此為一個初始化為空的多重集合,第二個建構函式則指定比較函式的型別 (Comp) ,用於建立元素順序,第三個則明確說明用到的配置器型別 (Al) 。 關鍵字 explicit 禁止某些型別的自動轉換。
第四個建構函式指定多重集合 Right的複本。
第五個建構函式藉由移動 Right 以指定多重集合的複本。
第六個、第七個、第八個建構函式會使用特定複製元素的 initializer_list。
接下來三個建構函式會複製具有比較函式中增加特定型別明確性的多重集合[First, Last)範圍。
範例
// multiset_ctor.cpp
// compile with: /EHsc
#include <set>
#include <iostream>
int main()
{
using namespace std;
//multiset <int>::iterator ms1_Iter, ms2_Iter, ms3_Iter;
multiset <int>::iterator ms4_Iter, ms5_Iter, ms6_Iter, ms7_Iter;
// Create an empty multiset ms0 of key type integer
multiset <int> ms0;
// Create an empty multiset ms1 with the key comparison
// function of less than, then insert 4 elements
multiset <int, less<int> > ms1;
ms1.insert(10);
ms1.insert(20);
ms1.insert(20);
ms1.insert(40);
// Create an empty multiset ms2 with the key comparison
// function of geater than, then insert 2 elements
multiset <int, less<int> > ms2;
ms2.insert(10);
ms2.insert(20);
// Create a multiset ms3 with the
// allocator of multiset ms1
multiset <int>::allocator_type ms1_Alloc;
ms1_Alloc = ms1.get_allocator();
multiset <int> ms3(less<int>(), ms1_Alloc);
ms3.insert(30);
// Create a copy, multiset ms4, of multiset ms1
multiset <int> ms4(ms1);
// Create a multiset ms5 by copying the range ms1[_First, _Last)
multiset <int>::const_iterator ms1_bcIter, ms1_ecIter;
ms1_bcIter = ms1.begin();
ms1_ecIter = ms1.begin();
ms1_ecIter++;
ms1_ecIter++;
multiset <int> ms5(ms1_bcIter, ms1_ecIter);
// Create a multiset ms6 by copying the range ms4[_First, _Last)
// and with the allocator of multiset ms2
multiset <int>::allocator_type ms2_Alloc;
ms2_Alloc = ms2.get_allocator();
multiset <int> ms6(ms4.begin(), ++ms4.begin(), less<int>(), ms2_Alloc);
cout << "ms1 =";
for (auto i : ms1)
cout << " " << i;
cout << endl;
cout << "ms2 =";
for (auto i : ms2)
cout << " " << i;
cout << endl;
cout << "ms3 =";
for (auto i : ms3)
cout << " " << i;
cout << endl;
cout << "ms4 =";
for (auto i : ms4)
cout << " " << i;
cout << endl;
cout << "ms5 =";
for (auto i : ms5)
cout << " " << i;
cout << endl;
cout << "ms6 =";
for (auto i : ms6)
cout << " " << i;
cout << endl;
// Create a multiset by moving ms5
multiset<int> ms7(move(ms5));
cout << "ms7 =";
for (auto i : ms7)
cout << " " << i;
cout << endl;
// Create a multiset with an initializer_list
multiset<int> ms8({1, 2, 3, 4});
cout << "ms8=";
for (auto i : ms8)
cout << " " << i;
cout << endl;
}
Output
ms1 = 10 20 20 40
ms2 = 10 20
ms3 = 30
ms4 = 10 20 20 40
ms5 = 10 20
ms6 = 10
ms7 = 10 20
ms8= 1 2 3 4
需求
標頭: <set>
命名空間: std