描述
C++ 函數std::multimap::erase()從多重映射中刪除元素範圍。
該成員函數減少了多圖的大小。
聲明
以下是 std::multimap::erase() 函數形式 std::map 頭的聲明。
C++11
iterator erase (const_iterator first, const_iterator last);
參數
first- 輸入迭代器到範圍內的初始位置。
last- 輸入迭代器到範圍內的最終位置。
返回值
返回最後一個刪除元素之後的迭代器。
異常
如果拋出異常,對容器沒有影響。
時間複雜度
之間的距離呈線性第一的至最後的。
示例
下麵的例子展示了 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;
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;
cout << "After erase operation iterator points to " << 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 After erase operation iterator points to b = 3
相關用法
- C++ Map emplace()用法及代碼示例
- C++ Map end()用法及代碼示例
- C++ Map empty()用法及代碼示例
- C++ Map equal_range()用法及代碼示例
- C++ Map emplace_hint()用法及代碼示例
- C++ Map rend()用法及代碼示例
- C++ Map crend()用法及代碼示例
- C++ Map multimap()用法及代碼示例
- C++ Map count()用法及代碼示例
- C++ Map clear()用法及代碼示例
- C++ Map cend()用法及代碼示例
- C++ Map crbegin()用法及代碼示例
- C++ Map at()用法及代碼示例
- C++ Map size()用法及代碼示例
- C++ Map get_allocator()用法及代碼示例
- C++ Map max_size()用法及代碼示例
- C++ Map begin()用法及代碼示例
- C++ Map map()用法及代碼示例
- C++ Map cbegin()用法及代碼示例
- C++ Map rbegin()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Map Library - erase() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。