首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::unordered_set::erase

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对象。

复杂性

给定实例cunordered_set*

1%29平均病例:不变,最坏情况:c.size()

2%29例平均病例:std::distance(first, last),最坏情况:c.size()

3%29例平均病例:c.count(key),最坏情况:c.size()

二次

代码语言:javascript
复制
#include <unordered_set>
#include <iostream>
int main()
{
    std::unordered_set<int> c = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    // erase all odd numbers from c
    for(auto it = c.begin(); it != c.end(); )
        if(*it % 2 == 1)
            it = c.erase(it);
        else
            ++it;
    for(int n : c)
        std::cout << n << ' ';
}

二次

可能的产出:

二次

代码语言:javascript
复制
2 4 6 8

二次

另见

clear

clears the contents (public member function)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券