std::unordered_map::erase
STD:无序[医]地图::擦除
iterator erase( const_iterator pos | (1) | (since C++11) |
---|---|---|
iterator erase( const_iterator first, const_iterator last | (2) | (since C++11) |
size_type erase( const key_type& key | (3) | (since C++11) |
从容器中移除指定的元素。
1%29移除pos
...
2%29移除范围内的元素。[first; last)
中的有效范围。*this
...
3%29删除元素%28,如果存在元素%29,其键等价于key
...
对擦除元素的引用和迭代器无效。其他迭代器和引用不会失效。
迭代器pos
必须是有效的和可撤销的。因此end()
迭代器%28有效,但不可取消引用%29不能用作pos
...
The order of the elements that are not erased is preserved (this makes it possible to erase individual elements while iterating through the container). | (since C++14) |
---|
参数
pos | - | iterator to the element to remove |
---|---|---|
first, last | - | range of elements to remove |
key | - | key value of the elements to remove |
返回值
1-2%,29,Iterator,在最后一次删除元素之后.
3%,29个元素被移除。
例外
1,2%29%28无29
3%29由Compare
对象。
复杂性
给定实例c
成unordered_map
*
1%29平均病例:不变,最坏情况:c.size()
2%29例平均病例:std::distance
(first, last)
,最坏情况:c.size()
3%29例平均病例:c.count(key)
,最坏情况:c.size()
例
二次
#include <unordered_map>
#include <iostream>
int main()
{
std::unordered_map<int, std::string> c = {{1, "one"}, {2, "two"}, {3, "three"},
{4, "four"}, {5, "five"}, {6, "six"}};
// erase all odd numbers from 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
二次
另见
clear | clears the contents (public member function) |
---|
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。