C++ Map 库 - crend() 函数
描述
C++ 函数 std::map::erase() 删除与键 k 关联的映射值。
声明
以下是 std::map::erase() 函数形式 std::map 头的声明。
C++98
size_type erase (const key_type& k);
C++11
size_type erase (const key_type& k);
参数
k − 要移除的元素的键。
返回值
返回移除的元素数量。
异常
引发与比较对象引发的相同异常。
时间复杂度
Logarithmic i.e. 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

