multiset 類別

「C++ 標準程式庫」multiset 類別可用來在集合中儲存及擷取資料,其中集合中所含的元素值不需要是唯一的,而且會作為自動排序資料時所依據的索引鍵值。 中 multiset 專案的索引鍵值可能不會直接變更。 相反地,必須刪除舊值,並插入具有新值的項目。

語法

template <class Key, class Compare =less <Key>, class Allocator =allocator <Key>>
class multiset

參數

Key
要存放在 multiset 中的項目資料類型。

Compare
型別,提供函式物件,可比較兩個元素值做為排序索引鍵,以判斷其在 中的 multiset相對順序。 小於<Key> 的二進位述詞是預設值。

在 C++14 中,您可以指定沒有類型參數的 std::less<>std::greater<> 述詞,以啟用異質查閱。 如需詳細資訊,請參閱 關聯容器 中的異質查閱。

Allocator
型別,表示預存配置器物件,該物件會封裝記憶體配置和解除分配的詳細 multiset數據。 預設值是 allocator<Key>

備註

C++ 標準連結庫 multiset 類別為:

  • 關聯的容器,為可變大小容器,支援項目值以關聯的索引鍵值為基礎、有效率的擷取。

  • 可逆轉的,因為它提供雙向的迭代器以存取其項目。

  • 已排序,因為其項目是依據指定的比較函式,由容器內的索引鍵值排序。

  • 其元素不需要有唯一索引鍵的多重,因此一個索引鍵值可以有多個與其相關聯的元素值。

  • 簡單關聯的容器,因為其項目值是其索引鍵值的。

  • 類別範本,因為它所提供的功能是泛型的,因此與專案所包含的特定數據類型無關。 使用的資料類型是在類別樣板中指定為參數 (和比較函式與配置器一起指定)。

類別所提供的 multiset 反覆運算器是雙向反覆運算器,但類別成員函 insert 式和 multiset 具有作為範本參數的較弱輸入反覆運算器的版本,其功能需求比雙向反覆運算器類別所保證的功能需求要小得多。 不同的迭代器概念因其功能的修改而形成關聯的系列。 每個迭代器概念有自己的一組需求,因此,使用它們的演算法必須將其假設限制為該迭代器類型的需求。 可假設輸入迭代器可能已取值來參考某個物件,而且可能會遞增為序列中的下一個迭代器。 這是一組基本功能,不過,已足以在類別成員函式的內容中有意義地溝通迭代器範圍 [ First, Last)。

選擇容器類型時,通常應根據應用程式所需的搜尋和插入類型。 關聯的容器已針對查閱、插入和移除作業最佳化。 明確支持這些作業的成員函式很有效率,在與容器中元素數目的對數成正比的一段時間內執行這些作業。 插入專案不會使反覆運算器失效,而移除元素只會使指向已移除元素的反覆運算器失效。

multiset當應用程式滿足將值與其索引鍵產生關聯的條件時,應該為所選擇的關聯容器。 的 multiset 元素可能是多個,並做為其本身的排序索引鍵,因此索引鍵不是唯一的。 例如,這種結構的模型是文字的已排序清單,其中文字可以出現多次。 如果不允許文字的多個項目,則集合是適當的容器結構。 如果唯一定義做為值附加至唯一關鍵字清單,則對應是包含這個資料的適當結構。 如果定義不是唯一 multimap 的,則會是選擇的容器。

multiset 藉由呼叫 類型的 Compare預存函式物件來排序它所控制的順序。 這個預存物件是可藉由呼叫成員函式 來存取的比較函式 key_comp。 一般而言,元素只需要小於可比較才能建立這個順序:因此,只要有任兩個元素,就可以判斷它們相等(從意義上說兩者都小於另一個元素),或一個小於另一個元素。 這會導致非對等元件之間的排序。 一個技術提示,比較函式是在標準數學概念上產生嚴格弱式順序的二元述詞。 二進位述詞 fx, y) 是函式物件,其具有兩個自變數物件 x 和 y以及 或 falsetrue傳回值。 如果二元述詞不具彈性、反對稱和可轉移性,而且等價是可轉移的,則對集合所加的排序是嚴格的弱式排序,其中當 f(x,y) 和 f(y,x) 和 fy,x) 都是 false 時,兩個物件 x 和 y 都是相等的。 如果更強的索引鍵相等條件取代等價條件,順序會變成總計 (也就是所有項目彼此相關的排序),因此相符的索引鍵之間將難以辨別。

在 C++14 中,您可以指定沒有類型參數的 std::less<>std::greater<> 述詞,以啟用異質查閱。 如需詳細資訊,請參閱 關聯容器 中的異質查閱。

建構函式

建構函式 描述
multiset 建構一個空的 multiset,或是指定之 multiset 的全部或部分複本。

Typedefs

類型名稱 描述
allocator_type allocator 物件之 multiset 類別的 typedef。
const_iterator 可以在 const 中讀取 multiset 項目的雙向迭代器的 typedef。
const_pointer const 中指向 multiset 項目的指標之 typedef。
const_reference 用於讀取和執行const作業之 中儲存之multiset元素的 typedefconst
const_reverse_iterator 可以在 const 中讀取任何 multiset 項目的雙向迭代器的 typedef。
difference_type 範圍 (介於迭代器所指的項目) 中 multiset 的項目數量的帶正負號整數 typedef。
iterator 雙向迭代器的 typedef,可以讀取或修改 multiset 中的任何項目。
key_compare 函式物件之 typedef,可比較兩個索引鍵以判斷兩個項目在 multiset 中的相對順序。
key_type 函式物件之 typedef,可比較兩個排序鍵以判斷兩個項目在 multiset 中的相對順序。
pointer multiset 中指向項目的指標之 typedef。
reference multiset 中預存項目的參考之 typedef。
reverse_iterator 雙向迭代器的 typedef,可以讀取或修改反轉 multiset 中的項目。
size_type 不帶正負號的整數類型,可以表示 multiset 中的項目數。
value_compare 可將兩個項目做為排序鍵進行比較之函式物件的 typedef,以判斷項目在 multiset 中的相對順序。
value_type typedef,在做為值的產能上,描述做為 multiset 的項目儲存的物件。

成員函式

成員函數 描述
begin 傳回指向 multiset 中的第一個項目的迭代器。
cbegin 傳回常數迭代器,為 multiset 中的第一個項目定址。
cend 傳回常數迭代器,為 multiset 中最後一個項目的下一個位置定址。
clear 清除 multiset 的所有項目。
containsC++20 檢查 中是否有具有指定索引鍵 multiset的專案。
count 傳回 multiset 中索引鍵符合指定為參數之索引鍵的項目數目。
crbegin 傳回常數迭代器,為反轉 multiset 中的第一個項目定址。
crend 傳回常數迭代器,為反轉 multiset 中最後一個項目的下一個位置定址。
emplace 將就地建構的項目插入 multiset 中。
emplace_hint 將就地建構的項目 (含位置提示) 插入 multiset 中。
empty 測試 multiset 是否為空白。
end 傳回 multiset 中,指向最後一個項目後面的位置之迭代器。
equal_range 傳回一對迭代器。 配對中第一個迭代器指向 multiset 中索引鍵大於指定索引鍵的第一個項目。 配對中第二個迭代器指向 multiset 中索引鍵等於或大於指定索引鍵的第一個項目。
erase 從指定的位置移除 multiset 中的項目或項目範圍,或移除符合指定之索引鍵的項目。
find 傳回迭代器,指向 multiset 中索引鍵等於指定索引鍵的第一個項目的位置。
get_allocator 傳回用來建構 allocatormultiset 物件複本。
insert 將項目或項目範圍插入至 multiset
key_comp 提供可比較兩個排序鍵的函式物件,以判斷兩個項目在 multiset 中的相對順序。
lower_bound 傳回迭代器,指向 multiset 中索引鍵等於或大於特定索引鍵的第一個項目。
max_size 傳回 multiset 的最大長度。
rbegin 傳回指向反轉 multiset 中的第一個項目的迭代器。
rend 傳回反轉 multiset 中,指向最後一個項目的下一個位置之迭代器。
size 傳回 multiset 中的項目數目。
swap 交換兩個 multiset 的項目。
upper_bound 傳回迭代器,指向 multiset 中索引鍵大於特定索引鍵的第一個項目。
value_comp 擷取 multiset 中用於排序項目值的比較物件之複本。

操作員

運算子 描述
operator= 用另一個 multiset 的複本取代 multiset 的項目。

需求

標頭:<set>

命名空間:std

multiset::allocator_type

型別,表示 物件的配置器類別multiset

typedef Allocator allocator_type;

備註

allocator_type 與樣板參數 Allocator 同義。

如需有關 Allocator 的詳細資訊,請參閱 multiset 類別主題的<備註>一節。

範例

如需使用範例,請參閱 範例get_allocatorallocator_type

multiset::begin

傳回迭代器,為 multiset 中的第一個項目定址。

const_iterator begin() const;

iterator begin();

傳回值

雙向反覆運算器,尋址物件是 中的 multiset 第一個專案,或是空多集後面的位置。

範例

// multiset_begin.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   multiset <int> ms1;
   multiset <int>::iterator ms1_Iter;
   multiset <int>::const_iterator ms1_cIter;

   ms1.insert( 1 );
   ms1.insert( 2 );
   ms1.insert( 3 );

   ms1_Iter = ms1.begin( );
   cout << "The first element of ms1 is " << *ms1_Iter << endl;

   ms1_Iter = ms1.begin( );
   ms1.erase( ms1_Iter );

   // The following 2 lines would err as the iterator is const
   // ms1_cIter = ms1.begin( );
   // ms1.erase( ms1_cIter );

   ms1_cIter = ms1.begin( );
   cout << "The first element of ms1 is now " << *ms1_cIter << endl;
}
The first element of ms1 is 1
The first element of ms1 is now 2

multiset::cbegin

傳回 const 迭代器,為範圍中的第一個項目定址。

const_iterator cbegin() const;

傳回值

const 雙向存取迭代器,指向範圍的第一個項目,或指向空白範圍結尾 (空白範圍 cbegin() == cend()) 之外的位置。

備註

使用 的 cbegin傳回值,就無法修改範圍中的專案。

您可以使用此成員函式取代 begin() 成員函式,以確保傳回值是 const_iterator。 通常,它是與 auto 類型推算關鍵字一起使用,如下列範例所示。 在此範例中,請考慮將 Container 視為任何支援 begin()cbegin() 且可修改 (非 const) 的容器類型。

auto i1 = Container.begin();
// i1 is Container<T>::iterator
auto i2 = Container.cbegin();

// i2 is Container<T>::const_iterator

multiset::cend

傳回 const 迭代器,為範圍中最後一個項目之外的位置定址。

const_iterator cend() const;

傳回值

指向範圍結尾之外的 const 雙向存取迭代器。

備註

cend 用來測試迭代器是否已超過其範圍結尾。

您可以使用此成員函式取代 end() 成員函式,以確保傳回值是 const_iterator。 通常,它是與 auto 類型推算關鍵字一起使用,如下列範例所示。 在此範例中,請考慮將 Container 視為任何支援 end()cend() 且可修改 (非 const) 的容器類型。

auto i1 = Container.end();
// i1 is Container<T>::iterator
auto i2 = Container.cend();

// i2 is Container<T>::const_iterator

cend 傳回的值不應該取值。

multiset::clear

清除 multiset 的所有項目。

void clear();

範例

// multiset_clear.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   multiset <int> ms1;

   ms1.insert( 1 );
   ms1.insert( 2 );

   cout << "The size of the multiset is initially "
        << ms1.size( ) << "." << endl;

   ms1.clear( );
   cout << "The size of the multiset after clearing is "
        << ms1.size( ) << "." << endl;
}
The size of the multiset is initially 2.
The size of the multiset after clearing is 0.

multiset::const_iterator

提供雙向迭代器的類型,這個迭代器可以讀取 const 中的 multiset 項目。

typedef implementation-defined const_iterator;

備註

類型 const_iterator 無法用來修改專案的值。

範例

如需使用 const_iterator的範例begin,請參閱 範例。

multiset::const_pointer

類型,提供 constmultiset 項目之指標。

typedef typename allocator_type::const_pointer const_pointer;

備註

類型 const_pointer 無法用來修改專案的值。

在大部分情況下, 反覆運算器 應該用來存取 物件中的 multiset 專案。

multiset::const_reference

型別,提供儲存在 中multiset之項目的參考const,以便讀取和執行const作業。

typedef typename allocator_type::const_reference const_reference;

範例

// multiset_const_ref.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   multiset <int> ms1;

   ms1.insert( 10 );
   ms1.insert( 20 );

   // Declare and initialize a const_reference &Ref1
   // to the 1st element
   const int &Ref1 = *ms1.begin( );

   cout << "The first element in the multiset is "
        << Ref1 << "." << endl;

   // The following line would cause an error because the
   // const_reference can't be used to modify the multiset
   // Ref1 = Ref1 + 5;
}
The first element in the multiset is 10.

multiset::const_reverse_iterator

提供雙向迭代器的類型,這個迭代器可以讀取 const 中的任何 multiset 項目。

typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

備註

const_reverse_iterator 別無法修改專案的值,並用來反向逐一查看 multiset

範例

如需如何宣告和使用 的const_reverse_iterator範例rend,請參閱範例。

multiset::contains

檢查 中是否有具有指定索引鍵 multiset的專案。

bool contains(const Key& key) const;
template<class K> bool contains(const K& key) const;

參數

K
索引鍵的類型。

key
要尋找的專案索引鍵值。

傳回值

true 如果在容器中找到專案,則為 ; false 否則。

備註

contains() 是 C++20 的新功能。 若要使用它,請指定 /std:c++20 或更新版本的編譯程序選項。

template<class K> bool contains(const K& key) const 只有在透明時 key_compare 才會參與多載解析。 如需詳細資訊,請參閱 關聯容器 中的異質查閱。

範例

// Requires /std:c++20 or /std:c++latest
#include <set>
#include <iostream>

int main()
{
    std::multiset<int> theMultiSet = {1, 2};

    std::cout << std::boolalpha; // so booleans show as 'true' or 'false'
    std::cout << theMultiSet.contains(2) << '\n';
    std::cout << theMultiSet.contains(3) << '\n';

    return 0;
}
true
false

multiset::count

傳回 multiset 中索引鍵符合參數指定之索引鍵的項目數目。

size_type count(const Key& key) const;

參數

key
要從 multiset比對之專案的索引鍵。

傳回值

multiset 排序索引鍵符合參數索引鍵的項目數目。

備註

成員函式會傳回下列範圍中的元素數目 x

[ lower_bound(key), upper_bound(key) )

範例

下列範例示範如何使用 multiset::count 成員函式。

// multiset_count.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main()
{
    using namespace std;
    multiset<int> ms1;
    multiset<int>::size_type i;

    ms1.insert(1);
    ms1.insert(1);
    ms1.insert(2);

    // Elements don't need to be unique in multiset,
    // so duplicates are allowed and counted.
    i = ms1.count(1);
    cout << "The number of elements in ms1 with a sort key of 1 is: "
         << i << "." << endl;

    i = ms1.count(2);
    cout << "The number of elements in ms1 with a sort key of 2 is: "
         << i << "." << endl;

    i = ms1.count(3);
    cout << "The number of elements in ms1 with a sort key of 3 is: "
         << i << "." << endl;
}
The number of elements in ms1 with a sort key of 1 is: 2.
The number of elements in ms1 with a sort key of 2 is: 1.
The number of elements in ms1 with a sort key of 3 is: 0.

multiset::crbegin

傳回 const 迭代器,用於定址反轉 Multiset 中的第一個項目。

const_reverse_iterator crbegin() const;

傳回值

const 反向雙向迭代器,用於定址反轉 Multiset 中的第一個元素,或用於定址未反轉 Multiset 中的最後一個元素。

備註

crbegin 與反向多重集搭配使用,就像begin與搭配使用一 multiset樣。

使用 的 crbegin傳回值, multiset 就無法修改 物件。

crbegin 可用來向後逐一查看 multiset

範例

// multiset_crbegin.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   multiset <int> ms1;
   multiset <int>::const_reverse_iterator ms1_crIter;

   ms1.insert( 10 );
   ms1.insert( 20 );
   ms1.insert( 30 );

   ms1_crIter = ms1.crbegin( );
   cout << "The first element in the reversed multiset is "
        << *ms1_crIter << "." << endl;
}
The first element in the reversed multiset is 30.

multiset::crend

傳回常數迭代器,定址對象是反轉 multiset 中最後一個元素後面的位置。

const_reverse_iterator crend() const;

傳回值

常數反轉雙向迭代器,定址對象是反轉 multiset 中最後一個元素後面的位置 (未反轉 multiset 中第一個元素前面的位置)。

備註

crend與反向多重集搭配使用,就像 搭配 使用一multisetend

使用 的 crend傳回值, multiset 就無法修改 物件。

crend 可以用來測試反轉迭代器是否已到達其 multiset 的結尾。

crend 傳回的值不應該取值。

範例

// multiset_crend.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main() {
   using namespace std;
   multiset <int> ms1;
   multiset <int>::const_reverse_iterator ms1_crIter;

   ms1.insert( 10 );
   ms1.insert( 20 );
   ms1.insert( 30 );

   ms1_crIter = ms1.crend( ) ;
   ms1_crIter--;
   cout << "The last element in the reversed multiset is "
        << *ms1_crIter << "." << endl;
}

multiset::difference_type

帶正負號的整數類型,可以用來表示範圍 (介於迭代器所指的項目) 中 multiset 的項目數。

typedef typename allocator_type::difference_type difference_type;

備註

difference_type 是透過容器的迭代器減去或遞增時會傳回的類型。 difference_type 通常用來代表迭代器 firstlast 之間範圍 [ first, last) 內的元素數目,包括 first 所指的元素以及上限到 last 所指元素 (但不包含此元素) 的元素範圍。

雖然 difference_type 適用於所有符合輸入反覆運算器需求的反覆運算器,其中包含可逆容器支援的雙向反覆運算器類別,例如 set,但反覆運算器之間的減法僅受隨機存取反覆運算器所提供的隨機存取反覆運算器支援,例如向量。

範例

// multiset_diff_type.cpp
// compile with: /EHsc
#include <iostream>
#include <set>
#include <algorithm>

int main( )
{
   using namespace std;

   multiset <int> ms1;
   multiset <int>::iterator ms1_Iter, ms1_bIter, ms1_eIter;

   ms1.insert( 20 );
   ms1.insert( 10 );
   ms1.insert( 20 );

   ms1_bIter = ms1.begin( );
   ms1_eIter = ms1.end( );

   multiset <int>::difference_type   df_typ5, df_typ10, df_typ20;

   df_typ5 = count( ms1_bIter, ms1_eIter, 5 );
   df_typ10 = count( ms1_bIter, ms1_eIter, 10 );
   df_typ20 = count( ms1_bIter, ms1_eIter, 20 );

   // The keys, and hence the elements, of a multiset aren't unique
   cout << "The number '5' occurs " << df_typ5
        << " times in multiset ms1.\n";
   cout << "The number '10' occurs " << df_typ10
        << " times in multiset ms1.\n";
   cout << "The number '20' occurs " << df_typ20
        << " times in multiset ms1.\n";

   // Count the number of elements in a multiset
   multiset <int>::difference_type  df_count = 0;
   ms1_Iter = ms1.begin( );
   while ( ms1_Iter != ms1_eIter)
   {
      df_count++;
      ms1_Iter++;
   }

   cout << "The number of elements in the multiset ms1 is: "
        << df_count << "." << endl;
}
The number '5' occurs 0 times in multiset ms1.
The number '10' occurs 1 times in multiset ms1.
The number '20' occurs 2 times in multiset ms1.
The number of elements in the multiset ms1 is: 3.

multiset::emplace

將就地建構 (未執行任何複製或移動作業) 的元素連同位置提示一起插入。

template <class... Args>
iterator emplace(Args&&... args);

參數

args
轉送的自變數,用來建構要插入 至的專案 multiset

傳回值

指向新插入之元素的迭代器。

備註

此函式不會使容器元素的參考無效,但是可能會使容器的所有迭代器無效。

在放置期間,如果擲回例外狀況,則不會修改容器的狀態。

範例

// multiset_emplace.cpp
// compile with: /EHsc
#include <set>
#include <string>
#include <iostream>

using namespace std;

template <typename S> void print(const S& s) {
    cout << s.size() << " elements: ";

    for (const auto& p : s) {
        cout << "(" << p << ") ";
    }

    cout << endl;
}

int main()
{
    multiset<string> s1;

    s1.emplace("Anna");
    s1.emplace("Bob");
    s1.emplace("Carmine");

    cout << "multiset modified, now contains ";
    print(s1);
    cout << endl;

    s1.emplace("Bob");

    cout << "multiset modified, now contains ";
    print(s1);
    cout << endl;
}

multiset::emplace_hint

將就地建構 (未執行任何複製或移動作業) 的元素連同位置提示一起插入。

template <class... Args>
iterator emplace_hint(
    const_iterator where,
    Args&&... args);

參數

args
轉送的自變數,用來建構要插入 至的專案 multiset

where
要開始搜尋正確的插入點的地方。 (若該點緊接於 where 之前,則可能會在分攤常數時間插入,而不是對數時間)。

傳回值

指向新插入之元素的迭代器。

備註

此函式不會使容器元素的參考無效,但是可能會使容器的所有迭代器無效。

在放置期間,如果擲回例外狀況,則不會修改容器的狀態。

如需程式碼範例,請參閱set::emplace_hint

multiset::empty

測試 multiset 是否為空白。

bool empty() const;

傳回值

true 如果 是空的 multiset ,則為 , false 如果 為空,則 multiset 為 。

範例

// multiset_empty.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main()
{
   using namespace std;
   multiset <int> ms1, ms2;
   ms1.insert ( 1 );

   if ( ms1.empty( ) )
      cout << "The multiset ms1 is empty." << endl;
   else
      cout << "The multiset ms1 is not empty." << endl;

   if ( ms2.empty( ) )
      cout << "The multiset ms2 is empty." << endl;
   else
      cout << "The multiset ms2 is not empty." << endl;
}
The multiset ms1 is not empty.
The multiset ms2 is empty.

multiset::end

傳回超出結尾 (past-the-end) 迭代器。

const_iterator end() const;

iterator end();

傳回值

超出結尾迭代器。 如果 multiset 是空的,則 multiset::end() == multiset::begin()

備註

end 是用來測試反覆運算器是否已通過其 multiset 的結尾。

end 傳回的值不應該取值。

如需程式碼範例,請參閱multiset::find

multiset::equal_range

傳回成對的迭代器,分別指向 multiset 中索引鍵大於特定索引鍵的第一個項目,以及指向 multiset 中索引鍵等於或大於該索引鍵的第一個項目。

pair <const_iterator, const_iterator> equal_range (const Key& key) const;

pair <iterator, iterator> equal_range (const Key& key);

參數

key
要與所搜尋專案排序索引鍵比較的 multiset 自變數索引鍵。

傳回值

一對反覆運算器,讓第一個是 lower_bound 索引鍵的 ,而第二個則是 upper_bound 索引鍵的 。

若要存取成員函式所傳回之 pr 配對的第一個迭代器,請使用 pr. first,若要取值下限反覆運算器,請使用 *( prfirst 若要存取成員函式所傳回之配對 pr 的第二個迭代器,請使用 pr. second若要取值上限反覆運算器,請使用 *( prsecond

範例

// multiset_equal_range.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   typedef multiset<int, less<int> > IntSet;
   IntSet ms1;
   multiset <int> :: const_iterator ms1_RcIter;

   ms1.insert( 10 );
   ms1.insert( 20 );
   ms1.insert( 30 );

   pair <IntSet::const_iterator, IntSet::const_iterator> p1, p2;
   p1 = ms1.equal_range( 20 );

   cout << "The upper bound of the element with "
        << "a key of 20 in the multiset ms1 is: "
        << *( p1.second ) << "." << endl;

   cout << "The lower bound of the element with "
        << "a key of 20 in the multiset ms1 is: "
        << *( p1.first ) << "." << endl;

   // Compare the upper_bound called directly
   ms1_RcIter = ms1.upper_bound( 20 );
   cout << "A direct call of upper_bound( 20 ) gives "
        << *ms1_RcIter << "," << endl
        << "matching the 2nd element of the pair"
        << " returned by equal_range( 20 )." << endl;

   p2 = ms1.equal_range( 40 );

   // If no match is found for the key,
   // both elements of the pair return end( )
   if ( ( p2.first == ms1.end( ) ) && ( p2.second == ms1.end( ) ) )
      cout << "The multiset ms1 doesn't have an element "
              << "with a key less than 40." << endl;
   else
      cout << "The element of multiset ms1 with a key >= 40 is: "
                << *( p1.first ) << "." << endl;
}
The upper bound of the element with a key of 20 in the multiset ms1 is: 30.
The lower bound of the element with a key of 20 in the multiset ms1 is: 20.
A direct call of upper_bound( 20 ) gives 30,
matching the 2nd element of the pair returned by equal_range( 20 ).
The multiset ms1 doesn't have an element with a key less than 40.

multiset::erase

從指定的位置移除 multiset 中的項目或項目範圍,或移除符合指定之索引鍵的項目。

iterator erase(
    const_iterator Where);

iterator erase(
    const_iterator First,
    const_iterator Last);

size_type erase(
    const key_type& Key);

參數

Where
要移除之項目的位置。

First
要移除之第一個項目的位置。

Last
緊接在要移除之最後一個項目後面的位置。

key
要移除之項目的索引鍵值。

傳回值

前兩個成員函式的雙向反覆運算器,指定任何移除之元素以外的第一個元素,或如果不存在這類專案,則為 結尾的專案 multiset

針對第三個成員函式,傳回已從 multiset中移除的項目數目。

備註

如需程式碼範例,請參閱set::erase

multiset::find

傳回反覆運算器,這個反覆運算器會參考 中索引鍵相當於指定索引鍵的 元素 multiset 位置。

iterator find(const Key& key);

const_iterator find(const Key& key) const;

參數

key
要與所搜尋專案排序索引鍵相符的 multiset 索引鍵值。

傳回值

反覆運算器,參考具有指定索引鍵的專案位置,如果找不到索引鍵相符專案,則為 ( 中multisetmultiset::end()最後一個專案後置的位置。

備註

成員函式會傳回反覆運算器,其索引鍵等於二元述詞下的 multiset 自變數 key ,該述詞會根據小於可比較性關聯引發排序。

如果 的傳回值 find 指派給 const_iteratormultiset 則無法修改 物件。 如果 的 find 傳回值指派給 iteratormultiset 則可以修改 物件

範例

// compile with: /EHsc /W4 /MTd
#include <set>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

template <typename T> void print_elem(const T& t) {
    cout << "(" << t << ") ";
}

template <typename T> void print_collection(const T& t) {
    cout << t.size() << " elements: ";

    for (const auto& p : t) {
        print_elem(p);
    }
    cout << endl;
}

template <typename C, class T> void findit(const C& c, T val) {
    cout << "Trying find() on value " << val << endl;
    auto result = c.find(val);
    if (result != c.end()) {
        cout << "Element found: "; print_elem(*result); cout << endl;
    } else {
        cout << "Element not found." << endl;
    }
}

int main()
{
    multiset<int> s1({ 40, 45 });
    cout << "The starting multiset s1 is: " << endl;
    print_collection(s1);

    vector<int> v;
    v.push_back(43);
    v.push_back(41);
    v.push_back(46);
    v.push_back(42);
    v.push_back(44);
    v.push_back(44); // attempt a duplicate

    cout << "Inserting the following vector data into s1: " << endl;
    print_collection(v);

    s1.insert(v.begin(), v.end());

    cout << "The modified multiset s1 is: " << endl;
    print_collection(s1);
    cout << endl;
    findit(s1, 45);
    findit(s1, 6);
}

multiset::get_allocator

傳回用來建構 multiset的配置器對象的複本。

allocator_type get_allocator() const;

傳回值

所使用的 multiset配置器。

備註

類別的配置 multiset 器會指定類別管理記憶體的方式。 C++ 標準程式庫容器類別隨附的預設配置器,足以滿足大多數程式設計需求。 撰寫和使用您自己的配置器類別是進階 C++ 主題。

範例

// multiset_get_allocator.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   multiset <int>::allocator_type ms1_Alloc;
   multiset <int>::allocator_type ms2_Alloc;
   multiset <double>::allocator_type ms3_Alloc;
   multiset <int>::allocator_type ms4_Alloc;

   // The following lines declare objects
   // that use the default allocator.
   multiset <int> ms1;
   multiset <int, allocator<int> > ms2;
   multiset <double, allocator<double> > ms3;

   cout << "The number of integers that can be allocated"
        << endl << "before free memory is exhausted: "
        << ms2.max_size( ) << "." << endl;

   cout << "The number of doubles that can be allocated"
        << endl << "before free memory is exhausted: "
        << ms3.max_size( ) <<  "." << endl;

   // The following lines create a multiset ms4
   // with the allocator of multiset ms1
   ms1_Alloc = ms1.get_allocator( );
   multiset <int> ms4( less<int>( ), ms1_Alloc );
   ms4_Alloc = ms4.get_allocator( );

   // Two allocators are interchangeable if
   // storage allocated from each can be
   // deallocated with the other
   if( ms1_Alloc == ms4_Alloc )
   {
      cout << "Allocators are interchangeable."
           << endl;
   }
   else
   {
      cout << "Allocators are not interchangeable."
           << endl;
   }
}

multiset::insert

將項目或項目範圍插入至 multiset

// (1) single element
pair<iterator, bool> insert(
    const value_type& Val);

// (2) single element, perfect forwarded
template <class ValTy>
pair<iterator, bool>
insert(
    ValTy&& Val);

// (3) single element with hint
iterator insert(
    const_iterator Where,
    const value_type& Val);

// (4) single element, perfect forwarded, with hint
template <class ValTy>
iterator insert(
    const_iterator Where,
    ValTy&& Val);

// (5) range
template <class InputIterator>
void insert(
    InputIterator First,
    InputIterator Last);

// (6) initializer list
void insert(
    initializer_list<value_type>
IList);

參數

Val
要插入至的專案 multiset值。

Where
要開始搜尋正確的插入點的地方。 (若該點緊接於 Where 之前,則可能會在分攤常數時間插入,而不是對數時間)。

ValTy
樣板參數,指定 可用來建構 的 元素的value_type自變數類型multiset,並以完美轉送Val做為自變數。

First
要複製之第一個元素的位置。

Last
要複製之最一個元素後方的位置。

1
範本函式自變數,其符合指向可用來建構 1 個物件之型別元素的需求input_iterator_tag

IList
initializer_list要從複製項目的 。

傳回值

單一元素插入成員函式 (1) 和 (2), 會將反覆運算器傳回插入新專案的位置 multiset

單一元素與提示成員函式 (3) 和 (4), 會傳回反覆運算器,指向插入 multiset新元素的位置。

備註

此函式不會使指標或參考無效,但是可能會使容器的所有迭代器無效。

在只插入一個項目期間,如果擲回例外狀況,則不會修改容器的狀態。 在插入多個元素期間,若擲出例外狀況,則容器會處於未指定但有效的狀態。

value_type容器的 是屬於容器的 typedef,而 set multiset<V>::value_type 的 類型const V為 。

範圍成員函式 (5) 會將專案值序列插入對應至 multiset 範圍 [First, Last)中反覆運算器所尋址的每個元素,因此 Last 不會插入。 容器成員函式 end() 是指容器中最後一個元素後方的位置;例如,陳述式 s.insert(v.begin(), v.end()); 會將 v 的所有元素插入至 s

初始化表示式清單成員函式 (6) 會使用 initializer_list 將項目 multiset複製到 。

若要插入就地建構的專案,也就是不會執行任何複製或移動作業,請參閱 multiset::emplacemultiset::emplace_hint

範例

// multiset_insert.cpp
// compile with: /EHsc
#include <set>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

template <typename S> void print(const S& s) {
    cout << s.size() << " elements: ";

    for (const auto& p : s) {
        cout << "(" << p << ") ";
    }

    cout << endl;
}

int main()
{
    // insert single values
    multiset<int> s1;
    // call insert(const value_type&) version
    s1.insert({ 1, 10 });
    // call insert(ValTy&&) version
    s1.insert(20);

    cout << "The original multiset values of s1 are:" << endl;
    print(s1);

    // intentionally attempt a duplicate, single element
    s1.insert(1);
    cout << "The modified multiset values of s1 are:" << endl;
    print(s1);
    cout << endl;

    // single element, with hint
    s1.insert(s1.end(), 30);
    cout << "The modified multiset values of s1 are:" << endl;
    print(s1);
    cout << endl;

    // The templatized version inserting a jumbled range
    multiset<int> s2;
    vector<int> v;
    v.push_back(43);
    v.push_back(294);
    v.push_back(41);
    v.push_back(330);
    v.push_back(42);
    v.push_back(45);

    cout << "Inserting the following vector data into s2:" << endl;
    print(v);

    s2.insert(v.begin(), v.end());

    cout << "The modified multiset values of s2 are:" << endl;
    print(s2);
    cout << endl;

    // The templatized versions move-constructing elements
    multiset<string>  s3;
    string str1("blue"), str2("green");

    // single element
    s3.insert(move(str1));
    cout << "After the first move insertion, s3 contains:" << endl;
    print(s3);

    // single element with hint
    s3.insert(s3.end(), move(str2));
    cout << "After the second move insertion, s3 contains:" << endl;
    print(s3);
    cout << endl;

    multiset<int> s4;
    // Insert the elements from an initializer_list
    s4.insert({ 4, 44, 2, 22, 3, 33, 1, 11, 5, 55 });
    cout << "After initializer_list insertion, s4 contains:" << endl;
    print(s4);
    cout << endl;
}

multiset::iterator

型別,提供常數 雙向反覆運算器 ,可讀取 中的任何 multiset專案。

typedef implementation-defined iterator;

範例

如需如何宣告及使用 iterator的範例,請參閱開頭的範例。

multiset::key_comp

擷取 multiset 中用來排序索引鍵的比較物件之複本。

key_compare key_comp() const;

傳回值

傳回 用來排序其專案的函式物件 multiset ,這是樣板參數 Compare

如需有關 Compare 的詳細資訊,請參閱 multiset 類別主題的<備註>一節。

備註

預存物件會定義成員函式:

bool operator( const Key&x, const Key&y);

如果 x 在排序次序中絕對位於 y 的前面,此函式就會傳回 true。

value_compare 都是key_compare樣板參數 Compare的同義字。 這兩種類型都針對類別集和 multiset 提供,其中它們完全相同,以便與類別對應和 multimap 相容,其中它們相異。

範例

// multiset_key_comp.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;

   multiset <int, less<int> > ms1;
   multiset <int, less<int> >::key_compare kc1 = ms1.key_comp( ) ;
   bool result1 = kc1( 2, 3 ) ;
   if( result1 == true )
   {
      cout << "kc1( 2,3 ) returns value of true, "
           << "where kc1 is the function object of s1."
           << endl;
   }
   else
   {
      cout << "kc1( 2,3 ) returns value of false "
           << "where kc1 is the function object of ms1."
           << endl;
   }

   multiset <int, greater<int> > ms2;
   multiset <int, greater<int> >::key_compare kc2 = ms2.key_comp( ) ;
   bool result2 = kc2( 2, 3 ) ;
   if( result2 == true )
   {
      cout << "kc2( 2,3 ) returns value of true, "
           << "where kc2 is the function object of ms2."
           << endl;
   }
   else
   {
      cout << "kc2( 2,3 ) returns value of false, "
           << "where kc2 is the function object of ms2."
           << endl;
   }
}
kc1( 2,3 ) returns value of true, where kc1 is the function object of s1.
kc2( 2,3 ) returns value of false, where kc2 is the function object of ms2.

multiset::key_compare

類型,提供可以比較兩個排序鍵的函式物件,以判斷兩個項目在 multiset 中的相對順序。

typedef Compare key_compare;

備註

key_compare 與樣板參數 Compare 同義。

如需 的詳細資訊Compare,請參閱類別主題的multiset一節。

範例

如需如何宣告和使用 key_compare的範例key_comp,請參閱範例。

multiset::key_type

型別,提供函式物件,可比較排序索引鍵,以判斷 中 multiset兩個項目的相對順序。

typedef Key key_type;

備註

key_type 與樣板參數 Key 同義。

如需 的詳細資訊Key,請參閱類別主題的multiset一節。

範例

如需如何宣告和使用 key_type的範例value_type,請參閱範例。

multiset::lower_bound

傳回迭代器,指向 multiset 中索引鍵等於或大於特定索引鍵的第一個項目。

const_iterator lower_bound(const Key& key) const;

iterator lower_bound(const Key& key);

參數

key
要與所搜尋專案排序索引鍵比較的 multiset 自變數索引鍵。

傳回值

iteratorconst_iterator ,尋址物件為索引鍵等於或大於自變數索引鍵之 中multiset專案的位置,如果找不到索引鍵相符專案,則尋址中最後multiset一個項目之後的位置。

範例

// multiset_lower_bound.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   multiset <int> ms1;
   multiset <int> :: const_iterator ms1_AcIter, ms1_RcIter;

   ms1.insert( 10 );
   ms1.insert( 20 );
   ms1.insert( 30 );

   ms1_RcIter = ms1.lower_bound( 20 );
   cout << "The element of multiset ms1 with a key of 20 is: "
        << *ms1_RcIter << "." << endl;

   ms1_RcIter = ms1.lower_bound( 40 );

   // If no match is found for the key, end( ) is returned
   if ( ms1_RcIter == ms1.end( ) )
      cout << "The multiset ms1 doesn't have an element "
           << "with a key of 40." << endl;
   else
      cout << "The element of multiset ms1 with a key of 40 is: "
           << *ms1_RcIter << "." << endl;

   // The element at a specific location in the multiset can be
   // found using a dereferenced iterator addressing the location
   ms1_AcIter = ms1.end( );
   ms1_AcIter--;
   ms1_RcIter = ms1.lower_bound( *ms1_AcIter );
   cout << "The element of ms1 with a key matching "
        << "that of the last element is: "
        << *ms1_RcIter << "." << endl;
}
The element of multiset ms1 with a key of 20 is: 20.
The multiset ms1 doesn't have an element with a key of 40.
The element of ms1 with a key matching that of the last element is: 30.

multiset::max_size

傳回 multiset 的最大長度。

size_type max_size() const;

傳回值

的最大可能長度 multiset

範例

// multiset_max_size.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   multiset <int> ms1;
   multiset <int>::size_type i;

   i = ms1.max_size( );
   cout << "The maximum possible length "
        << "of the multiset is " << i << "." << endl;
}

multiset::multiset

建構一個空的 multiset,或是其他 multiset 的全部或部分複本。

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
要用於此 multiset 物件的儲存體配置器類別,預設為 Allocator

Comp
類型為 const Compare 並用來排序 multiset 中元素的比較函式,預設為 Compare

Right
multiset 構的 multiset 為複本的 。

First
要複製的元素範圍中第一個元素的位置。

Last
超出要複製之元素範圍的第一個元素的位置。

IList
initializer_list要從複製項目的 。

備註

所有建構函式都會儲存一種配置器物件類型,該物件會管理的 multiset 記憶體記憶體記憶體記憶體,且稍後可藉由呼叫 get_allocator傳回。 在類別宣告以及用來取代替代配置器的前置處理巨集中,經常會省略 allocator 參數。

所有建構函式都會將其 multiset 初始化。

所有建構函式都會儲存 Compare 類型的函式物件,用來在 的 multiset 索引鍵之間建立順序,稍後可藉由呼叫 key_comp傳回。

前三個建構函式會指定空的初始多重集,第二個建構函式會指定要用於建立元素順序的比較函式類型Comp,第三個明確指定要使用的配置器類型 (Al)。 關鍵字 explicit 會隱藏某些類型的自動類型轉換。

第四個建構函式會指定的 multisetRight複本。

第五個建構函式藉由移動 Right來指定 的multiset複本。

第 6、7 和 8 個建構函式會指定要從中複製元素的initializer_list。

接下來的三個建構函式會在指定比較函式和配置器類型時,複製 具有遞增明確性的 範圍[First, Last)multiset

範例

// 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 greater 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;
}

multiset::operator=

將此 multiset 的元素以另一個 multiset 的元素取代。

multiset& operator=(const multiset& right);

multiset& operator=(multiset&& right);

參數

Right
要從中複製或移除元素的 multiset

備註

operator= 會依據所使用的參考類型 (lvalue 或 rvalue),將 Right 中的元素複製或移到這個 multiset。 在這個位於 operator= 之前 multiset 中的所有元素都會被捨棄。

範例

// multiset_operator_as.cpp
// compile with: /EHsc
#include <multiset>
#include <iostream>

int main( )
   {
   using namespace std;
   multiset<int> v1, v2, v3;
   multiset<int>::iterator iter;

   v1.insert(10);

   cout << "v1 = " ;
   for (iter = v1.begin(); iter != v1.end(); iter++)
      cout << *iter << " ";
   cout << endl;

   v2 = v1;
   cout << "v2 = ";
   for (iter = v2.begin(); iter != v2.end(); iter++)
      cout << *iter << " ";
   cout << endl;

// move v1 into v2
   v2.clear();
   v2 = move(v1);
   cout << "v2 = ";
   for (iter = v2.begin(); iter != v2.end(); iter++)
      cout << *iter << " ";
   cout << endl;
   }

multiset::pointer

類型,其提供 multiset 中項目的指標。

typedef typename allocator_type::pointer pointer;

備註

pointer 別可用來修改專案的值。

在大部分情況下, 反覆運算器 應該用來存取 物件中的 multiset 專案。

multiset::rbegin

傳回迭代器,用於定址反轉 Multiset 中的第一個項目。

const_reverse_iterator rbegin() const;

reverse_iterator rbegin();

傳回值

反向雙向反覆運算器,尋址反 multiset 轉中的第一個專案,或尋址未反 multiset轉 中的最後一個專案。

備註

rbegin 與反轉 multiset 搭配使用,就像 rbegin 搭配使用一 multiset樣。

如果 的傳回值 rbegin 指派給 const_reverse_iterator,則 multiset 無法修改 物件。 如果 的 rbegin 傳回值指派給 reverse_iteratormultiset 則可以修改 物件。

rbegin 可用來向後逐一查看 multiset

範例

// multiset_rbegin.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   multiset <int> ms1;
   multiset <int>::iterator ms1_Iter;
   multiset <int>::reverse_iterator ms1_rIter;

   ms1.insert( 10 );
   ms1.insert( 20 );
   ms1.insert( 30 );

   ms1_rIter = ms1.rbegin( );
   cout << "The first element in the reversed multiset is "
        << *ms1_rIter << "." << endl;

   // begin can be used to start an iteration
   // through a multiset in a forward order
   cout << "The multiset is:";
   for ( ms1_Iter = ms1.begin( ) ; ms1_Iter != ms1.end( ); ms1_Iter++ )
      cout << " " << *ms1_Iter;
   cout << endl;

   // rbegin can be used to start an iteration
   // through a multiset in a reverse order
   cout << "The reversed multiset is:";
   for ( ms1_rIter = ms1.rbegin( ) ; ms1_rIter != ms1.rend( ); ms1_rIter++ )
      cout << " " << *ms1_rIter;
   cout << endl;

   // a multiset element can be erased by dereferencing to its key
   ms1_rIter = ms1.rbegin( );
   ms1.erase ( *ms1_rIter );

   ms1_rIter = ms1.rbegin( );
   cout << "After the erasure, the first element "
        << "in the reversed multiset is "<< *ms1_rIter << "."
        << endl;
}
The first element in the reversed multiset is 30.
The multiset is: 10 20 30
The reversed multiset is: 30 20 10
After the erasure, the first element in the reversed multiset is 20.

multiset::reference

類型,提供儲存在 multiset 中之項目的參考。

typedef typename allocator_type::reference reference;

範例

// multiset_ref.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   multiset <int> ms1;

   ms1.insert( 10 );
   ms1.insert( 20 );

   // Declare and initialize a reference &Ref1 to the 1st element
   const int &Ref1 = *ms1.begin( );

   cout << "The first element in the multiset is "
        << Ref1 << "." << endl;
}
The first element in the multiset is 10.

multiset::rend

傳回迭代器,為反轉 multiset 中最後一個項目的下一個位置定址。

const_reverse_iterator rend() const;

reverse_iterator rend();

傳回值

反向雙向反覆運算器,尋址對像是反轉中最後一個項目之後的位置(在未反multisetmultiset的第一個元素之前的位置)。

備註

rend與反向多重集搭配使用,就像 搭配 使用一multisetend

如果 的傳回值 rend 指派給 const_reverse_iterator,則 multiset 無法修改 物件。 如果 的 rend 傳回值指派給 reverse_iteratormultiset 則可以修改 物件。

rend 可以用來測試反轉迭代器是否已到達其 multiset 的結尾。

rend 傳回的值不應該取值。

範例

// multiset_rend.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main() {
   using namespace std;
   multiset <int> ms1;
   multiset <int>::iterator ms1_Iter;
   multiset <int>::reverse_iterator ms1_rIter;
   multiset <int>::const_reverse_iterator ms1_crIter;

   ms1.insert( 10 );
   ms1.insert( 20 );
   ms1.insert( 30 );

   ms1_rIter = ms1.rend( ) ;
   ms1_rIter--;
   cout << "The last element in the reversed multiset is "
        << *ms1_rIter << "." << endl;

   // end can be used to terminate an iteration
   // through a multiset in a forward order
   cout << "The multiset is: ";
   for ( ms1_Iter = ms1.begin( ) ; ms1_Iter != ms1.end( ); ms1_Iter++ )
      cout << *ms1_Iter << " ";
   cout << "." << endl;

   // rend can be used to terminate an iteration
   // through a multiset in a reverse order
   cout << "The reversed multiset is: ";
   for ( ms1_rIter = ms1.rbegin( ) ; ms1_rIter != ms1.rend( ); ms1_rIter++ )
      cout << *ms1_rIter << " ";
   cout << "." << endl;

   ms1_rIter = ms1.rend( );
   ms1_rIter--;
   ms1.erase ( *ms1_rIter );

   ms1_rIter = ms1.rend( );
   --ms1_rIter;
   cout << "After the erasure, the last element in the "
        << "reversed multiset is " << *ms1_rIter << "." << endl;
}

multiset::reverse_iterator

一種類型,提供可讀取或修改反轉 multiset 中元素的雙向迭代器。

typedef std::reverse_iterator<iterator> reverse_iterator;

備註

類型 reverse_iterator 是用來反向逐一查看 multiset

範例

rbegin如需如何宣告和使用 reverse_iterator的範例,請參閱 範例。

multiset::size

傳回 multiset 中項目的數目。

size_type size() const;

傳回值

的目前長度 multiset

範例

// multiset_size.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   multiset <int> ms1;
   multiset <int> :: size_type i;

   ms1.insert( 1 );
   i = ms1.size( );
   cout << "The multiset length is " << i << "." << endl;

   ms1.insert( 2 );
   i = ms1.size( );
   cout << "The multiset length is now " << i << "." << endl;
}
The multiset length is 1.
The multiset length is now 2.

multiset::size_type

不帶正負號的整數類型,可以表示 multiset 中的項目數。

typedef typename allocator_type::size_type size_type;

範例

size如需如何宣告和使用範例,請參閱範例size_type

multiset::swap

交換兩個 multiset 的元素。

void swap(
    multiset<Key, Compare, Allocator>& right);

參數

Right
提供要與目標 multiset 交換之元素的引數 multiset。

備註

任何參考、指標或迭代器只要指定的元素是在交換元素的兩個 multiset 中,成員函式就不會使其失效。

範例

// multiset_swap.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   multiset <int> ms1, ms2, ms3;
   multiset <int>::iterator ms1_Iter;

   ms1.insert( 10 );
   ms1.insert( 20 );
   ms1.insert( 30 );
   ms2.insert( 100 );
   ms2.insert( 200 );
   ms3.insert( 300 );

   cout << "The original multiset ms1 is:";
   for ( ms1_Iter = ms1.begin( ); ms1_Iter != ms1.end( ); ms1_Iter++ )
      cout << " " << *ms1_Iter;
   cout << "." << endl;

   // This is the member function version of swap
   ms1.swap( ms2 );

   cout << "After swapping with ms2, list ms1 is:";
   for ( ms1_Iter = ms1.begin( ); ms1_Iter != ms1.end( ); ms1_Iter++ )
      cout << " " << *ms1_Iter;
   cout << "." << endl;

   // This is the specialized template version of swap
   swap( ms1, ms3 );

   cout << "After swapping with ms3, list ms1 is:";
   for ( ms1_Iter = ms1.begin( ); ms1_Iter != ms1.end( ); ms1_Iter++ )
      cout << " " << *ms1_Iter;
   cout   << "." << endl;
}
The original multiset ms1 is: 10 20 30.
After swapping with ms2, list ms1 is: 100 200.
After swapping with ms3, list ms1 is: 300.

multiset::upper_bound

傳回迭代器,指向 multiset 中索引鍵大於特定索引鍵的第一個項目。

const_iterator upper_bound(const Key& key) const;

iterator upper_bound(const Key& key);

參數

key
要與所搜尋專案排序索引鍵比較的 multiset 自變數索引鍵。

傳回值

反覆運算器,或 const_iterator ,尋址中索引鍵大於自變數索引鍵的專案位置multiset,如果找不到索引鍵相符專案,則尋址中最後multiset一個項目之後的位置。

範例

// multiset_upper_bound.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   multiset <int> ms1;
   multiset <int> :: const_iterator ms1_AcIter, ms1_RcIter;

   ms1.insert( 10 );
   ms1.insert( 20 );
   ms1.insert( 30 );

   ms1_RcIter = ms1.upper_bound( 20 );
   cout << "The first element of multiset ms1 with a key greater "
           << "than 20 is: " << *ms1_RcIter << "." << endl;

   ms1_RcIter = ms1.upper_bound( 30 );

   // If no match is found for the key, end( ) is returned
   if ( ms1_RcIter == ms1.end( ) )
      cout << "The multiset ms1 doesn't have an element "
              << "with a key greater than 30." << endl;
   else
      cout << "The element of multiset ms1 with a key > 40 is: "
           << *ms1_RcIter << "." << endl;

   // The element at a specific location in the multiset can be
   // found using a dereferenced iterator addressing the location
   ms1_AcIter = ms1.begin( );
   ms1_RcIter = ms1.upper_bound( *ms1_AcIter );
   cout << "The first element of ms1 with a key greater than"
        << endl << "that of the initial element of ms1 is: "
        << *ms1_RcIter << "." << endl;
}
The first element of multiset ms1 with a key greater than 20 is: 30.
The multiset ms1 doesn't have an element with a key greater than 30.
The first element of ms1 with a key greater than
that of the initial element of ms1 is: 20.

multiset::value_comp

擷取 multiset 中用於排序項目值之比較物件的複本。

value_compare value_comp() const;

傳回值

傳回 用來排序其專案的函式物件 multiset ,這是樣板參數 Compare

如需 的詳細資訊Compare,請參閱類別主題的multiset一節。

備註

預存物件會定義成員函式:

bool operator( const Key&_xVal, const Key&_yVal);

如果 _xVal 之前和 在 排序順序中不等於 _yVal ,則會傳回 true。

value_compare 都是key_compare樣板參數 Compare的同義字。 這兩種類型都針對類別集和 multiset提供,其中它們完全相同,以便與類別對應和 multimap相異。

範例

// multiset_value_comp.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;

   multiset <int, less<int> > ms1;
   multiset <int, less<int> >::value_compare vc1 = ms1.value_comp( );
   bool result1 = vc1( 2, 3 );
   if( result1 == true )
   {
      cout << "vc1( 2,3 ) returns value of true, "
           << "where vc1 is the function object of ms1."
           << endl;
   }
   else
   {
      cout << "vc1( 2,3 ) returns value of false, "
           << "where vc1 is the function object of ms1."
           << endl;
   }

   set <int, greater<int> > ms2;
   set<int, greater<int> >::value_compare vc2 = ms2.value_comp( );
   bool result2 = vc2( 2, 3 );
   if( result2 == true )
   {
      cout << "vc2( 2,3 ) returns value of true, "
           << "where vc2 is the function object of ms2."
           << endl;
   }
   else
   {
      cout << "vc2( 2,3 ) returns value of false, "
           << "where vc2 is the function object of ms2."
           << endl;
   }
}
vc1( 2,3 ) returns value of true, where vc1 is the function object of ms1.
vc2( 2,3 ) returns value of false, where vc2 is the function object of ms2.

multiset::value_compare

型別,提供函式物件,可比較兩個排序索引鍵,以判斷其在 中的 multiset相對順序。

typedef key_compare value_compare;

備註

value_compare 與樣板參數 Compare 同義。

value_compare 都是key_compare樣板參數 Compare的同義字。 這兩種類型都針對類別集和 multiset提供,其中它們完全相同,以便與類別對應和 multimap相異。

如需有關 Compare 的詳細資訊,請參閱 multiset 類別主題的<備註>一節。

範例

如需如何宣告和使用 value_compare的範例value_comp,請參閱範例。

multiset::value_type

型別,描述儲存為項目的物件,以 multiset 做為其容量中的 值。

typedef Key value_type;

備註

value_type 與樣板參數 Key 同義。

value_type 都是key_type樣板參數 Key的同義字。 這兩種類型都針對類別集和 multiset 提供,其中它們完全相同,以便與類別對應和 multimap 相容,其中它們相異。

如需有關 Key 的詳細資訊,請參閱該主題的<備註>一節。

範例

// multiset_value_type.cpp
// compile with: /EHsc
#include <set>
#include <iostream>

int main( )
{
   using namespace std;
   multiset <int> ms1;
   multiset <int>::iterator ms1_Iter;

   multiset <int> :: value_type svt_Int;   // Declare value_type
   svt_Int = 10;             // Initialize value_type

   multiset <int> :: key_type skt_Int;   // Declare key_type
   skt_Int = 20;             // Initialize key_type

   ms1.insert( svt_Int );         // Insert value into s1
   ms1.insert( skt_Int );         // Insert key into s1

   // a multiset accepts key_types or value_types as elements
   cout << "The multiset has elements:";
   for ( ms1_Iter = ms1.begin( ) ; ms1_Iter != ms1.end( ); ms1_Iter++ )
      cout << " " << *ms1_Iter;
   cout << "." << endl;
}
The multiset has elements: 10 20.

另請參閱

容器
C++ 標準程式庫中的執行緒安全
C++ 標準程式庫參考