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



描述

C++ 函式std::multimap::erase() 從多重對映中刪除元素範圍。

此成員函式減少多重對映的大小。

宣告

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

C++98

void erase (iterator first, iterator last);

引數

  • first - 輸入迭代器,指向範圍的初始位置。

  • last - 輸入迭代器,指向範圍的最終位置。

返回值

異常

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

時間複雜度

在之間的距離上是線性的firstlast.

示例

以下示例顯示了 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;

   auto it = m.begin();
   ++it, ++it;

   m.erase(m.begin(), it);

   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
b = 3
c = 4
c = 5
map.htm
廣告