公有成員函式
<set>

std::multiset::multiset

empty (1)
explicit multiset (const key_compare& comp = key_compare(),                   const allocator_type& alloc = allocator_type());
range (2)
template <class InputIterator>  multiset (InputIterator first, InputIterator last,            const key_compare& comp = key_compare(),            const allocator_type& alloc = allocator_type());
copy (3)
multiset (const multiset& x);
empty (1)
explicit multiset (const key_compare& comp = key_compare(),                   const allocator_type& alloc = allocator_type());explicit multiset (const allocator_type& alloc);
range (2)
template <class InputIterator>  multiset (InputIterator first, InputIterator last,            const key_compare& comp = key_compare(),            const allocator_type& = allocator_type());
copy (3)
multiset (const multiset& x);multiset (const multiset& x, const allocator_type& alloc);
move (4)
multiset (multiset&& x);multiset (multiset&& x, const allocator_type& alloc);
initializer list (5)
multiset (initializer_list<value_type> il,          const key_compare& comp = key_compare(),          const allocator_type& alloc = allocator_type());
empty (1)
mutiset();explicit multiset (const key_compare& comp,                   const allocator_type& alloc = allocator_type());explicit multiset (const allocator_type& alloc);
range (2)
template <class InputIterator>  multiset (InputIterator first, InputIterator last,            const key_compare& comp = key_compare(),            const allocator_type& = allocator_type());template <class InputIterator>  multiset (InputIterator first, InputIterator last,            const allocator_type& = allocator_type());
copy (3)
multiset (const multiset& x);multiset (const multiset& x, const allocator_type& alloc);
move (4)
multiset (multiset&& x);multiset (multiset&& x, const allocator_type& alloc);
initializer list (5)
multiset (initializer_list<value_type> il,          const key_compare& comp = key_compare(),          const allocator_type& alloc = allocator_type());multiset (initializer_list<value_type> il,          const allocator_type& alloc = allocator_type());
構造 multiset
構造一個 multiset 容器物件,根據使用的建構函式版本初始化其內容

(1) 空容器建構函式(預設建構函式)
構造一個 容器,不包含任何元素。
(2) 範圍建構函式 ((2) range constructor)
構造一個容器,包含範圍[first,last)中的元素數量,每個元素都從該範圍中的相應元素構造。
(3) 複製建構函式
構造一個容器,包含 x 中所有元素的副本。

該容器會保留 alloccomp 的內部副本,這些副本在容器的整個生命週期中用於分配儲存和對元素進行排序。
複製建構函式 (3) 建立一個容器,該容器保留並使用 x分配器比較物件 的副本。

元素的儲存使用此 內部分配器 進行分配。

元素根據 比較物件 進行排序。
(1) 空容器建構函式(預設建構函式)
構造一個 容器,不包含任何元素。
(2) 範圍建構函式 ((2) range constructor)
構造一個容器,包含範圍[first,last)透過對範圍中的每個元素使用 emplace-constructed 來構造。
(3) 複製建構函式(以及帶分配器的複製)
構造一個容器,包含 x 中所有元素的副本。
(4) 移動建構函式(以及帶分配器的移動)
構造一個容器,該容器獲取 x 的元素。
如果指定了 alloc 且它與 x 的分配器不同,則元素將被移動。否則,不構造任何元素(它們的擁有權直接轉移)。
x 將被置於一個不確定但有效的狀態。
(5) 初始化列表建構函式
構造一個容器,包含 il 中所有元素的副本。

該容器會保留 alloc 的內部副本,用於分配和釋放其元素的儲存,以及根據其 allocator_traits 指定的方式構造和銷燬它們。如果建構函式未傳遞 alloc 引數,則使用預設構造的分配器,但以下情況除外:
- 複製建構函式 (3, 第一個簽名) 建立一個容器,該容器保留並使用透過呼叫 x 的分配器的相應 select_on_container_copy_construction 特性返回的分配器副本。
- 移動建構函式 (4, 第一個簽名) 獲取 x 的分配器。

該容器還保留 comp(或 x比較物件)的內部副本,該副本用於確定容器中元素的順序並檢查等價元素。

所有元素都透過呼叫具有適當引數的 allocator_traits::construct 來進行複製移動或以其他方式構造

元素根據 比較物件 進行排序。如果傳遞給建構函式的元素是等價的,則它們的相對順序將得到保留。

引數

comp
二元謂詞,它接受兩個與 multiset 中包含的元素型別相同的引數,並返回true如果第一個引數在其定義的嚴格弱序中排在第二個引數之前,則返回false否則為 false。
這應該是一個函式指標或函式物件。
成員型別key_compare是容器使用的內部比較物件型別,在 multiset 中定義為其第二個模板引數的別名(Compare).
如果已知key_compare使用預設的 less(沒有狀態),此引數無關緊要。
alloc
分配器物件。
容器會保留並使用此分配器的內部副本。
成員型別allocator_type是容器使用的內部分配器型別,在 multiset 中定義為其第三個模板引數的別名(Alloc).
如果已知allocator_type是預設 allocator 的例項化(沒有狀態),此引數無關緊要。
first, last
指向範圍內初始位置和最終位置的輸入迭代器。 使用的範圍是[first,last),它包括firstlast之間的所有元素,包括first指向的元素,但不包括last指向的元素。
函式模板引數InputIterator應為 輸入迭代器 型別,指向可以從中構造value_type物件的型別的元素。
x
另一個 multiset 物件,型別相同(具有相同的類模板引數T, CompareAlloc),其內容被複制或獲取。
il
il
這些物件是從初始化列表宣告符自動構造的。
成員型別value_type是容器中元素的型別,在 multiset 中定義為第一個模板引數的別名(T).

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// constructing multisets
#include <iostream>
#include <set>

bool fncomp (int lhs, int rhs) {return lhs<rhs;}

struct classcomp {
  bool operator() (const int& lhs, const int& rhs) const
  {return lhs<rhs;}
};

int main ()
{
  std::multiset<int> first;                          // empty multiset of ints

  int myints[]= {10,20,30,20,20};
  std::multiset<int> second (myints,myints+5);       // pointers used as iterators

  std::multiset<int> third (second);                 // a copy of second

  std::multiset<int> fourth (second.begin(), second.end());  // iterator ctor.

  std::multiset<int,classcomp> fifth;                // class as Compare

  bool(*fn_pt)(int,int) = fncomp;
  std::multiset<int,bool(*)(int,int)> sixth (fn_pt); // function pointer as Compare

  return 0;
}

該程式碼不產生任何輸出,但展示了構造 multiset 容器的幾種方式。

複雜度

常量,用於空建構函式 (1)移動建構函式 (4)(除非 allocx 的分配器不同)。
對於所有其他情況,如果提供的序列是有序的且具有相同的排序標準,則複雜度與迭代器之間的距離成線性關係。對於無序序列,複雜度與該距離成對數線性關係(N*logN)(排序,複製構造)。

迭代器有效性

移動建構函式 (4) 會使與 x 相關的所有迭代器、指標和引用失效(如果元素被移動)。

資料競爭

所有複製的元素都會被訪問。
移動建構函式 (4) 會修改 x

異常安全

強保證: 如果丟擲異常,則沒有任何效果。
如果對於元素構造不支援帶有適當引數的 allocator_traits::construct,或者如果由[first,last)無效,則會導致未定義行為

另見