std::multimap<Key,T,Compare,Allocator>::erase
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    
| (1) | ||
|   void erase( iterator pos );  | 
(C++11 前) | |
|   iterator erase( const_iterator pos );  | 
(C++11 起) | |
|   iterator erase( iterator pos );  | 
(C++17 起) | |
| (2) | ||
|   void erase( iterator first, iterator last );  | 
(C++11 前) | |
|   iterator erase( const_iterator first, const_iterator last );  | 
(C++11 起) | |
|   size_type erase( const key_type& key );  | 
(3) | |
从容器移除指定的元素。
1) 移除位于 
pos 的元素。2) 移除范围 
[first; last) 中的元素,它必须是 *this 中的合法范围。3) 移除关键等于 
key 的所有元素。指向被擦除元素的引用和迭代器被非法化。其他引用和迭代器不受影响。
迭代器 pos 必须合法且可解引用。从而 end() 迭代器(合法,但不可解引用)不能用作 pos 所用的值。
参数
| pos | - | 指向要移除的元素的迭代器 | 
| first, last | - | 要移除的元素范围 | 
| key | - | 要移除的元素关键值 | 
返回值
1-2) 后随最后被移除的元素的迭代器。
3) 被移除的元素数。
异常
1,2) 不抛出
3) 任何 
Compare 对象所抛的异常复杂度
给定 multimap 的实例 c :
1) 均摊常数
2) log(c.size()) + std::distance(first, last)
3) log(c.size()) + c.count(k)
示例
运行此代码
#include <map> #include <iostream> int main() { std::multimap<int, std::string> c = {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}, {6, "six"}}; // 从 c 擦除所有奇数 for(auto it = c.begin(); it != c.end(); ) if(it->first % 2 == 1) it = c.erase(it); else ++it; for(auto& p : c) std::cout << p.second << ' '; }
输出:
two four six
参阅
|   清除内容  (公开成员函数)  |