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



描述

C++ 函式std::map::erase() 刪除與鍵關聯的對映值k.

宣告

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

C++98

size_type erase (const key_type& k);

C++11

size_type erase (const key_type& k);

引數

k − 要刪除的元素的鍵。

返回值

返回刪除的元素個數。

異常

丟擲與 Compare 物件丟擲的相同的異常。

時間複雜度

對數,即 log(n)

示例

以下示例演示了 std::map::erase() 函式的使用。

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   /* Initializer_list constructor */
   map<char, int> m = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5},
            };

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

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

   m.erase('a');

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

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

   return 0;
}

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

Map contains following elements before erase operation
a = 1
b = 2
c = 3
d = 4
e = 5
Map contains following elements after erase operation
b = 2
c = 3
d = 4
e = 5
map.htm
廣告