C++ 對映庫 - erase() 函式



描述

C++ 函式std::multimap::erase() 從位置移除多重對映中的單個元素。

此成員函式將多重對映的大小減小一個。

宣告

以下是來自 std::map 標頭檔案的 std::multimap::erase() 函式的宣告。

C++98

void erase (iterator position);

引數

position − 要移除的元素的迭代器。

返回值

異常

如果丟擲異常,則對容器沒有影響。

時間複雜度

對數,即 O(log n)

示例

以下示例顯示了 std::multimap::erase() 函式的用法。

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   /* Multimap with duplicates */
   multimap<char, int> m {
            {'a', 1},
            {'a', 2},
            {'b', 3},
            {'c', 4},
            {'c', 5},
         };

   cout << "Multimap contains following elements before erase operation" << endl;

   for (auto it = m.begin(); it != m.end(); ++it)
      cout << it->first << " = " << it->second << endl;

   cout << endl << endl;

   m.erase(m.begin());

   cout << "Multimap contains following elements after erase operation" << endl;

   for (auto it = m.begin(); it != m.end(); ++it)
      cout << it->first << " = " << it->second << endl;

   return 0;
}

讓我們編譯並執行上面的程式,這將產生以下結果:

Multimap contains following elements before erase operation
a = 1
a = 2
b = 3
c = 4
c = 5

Multimap contains following elements after erase operation
a = 2
b = 3
c = 4
c = 5
map.htm
廣告