跳到主要内容

std::unordered_set erase() 方法

// (1) Non const version only
iterator erase( iterator pos );
iterator erase( const_iterator pos );

// (2) Non const version only
iterator erase( const_iterator first, const_iterator last );

// (3) Non const version only
size_type erase( const Key& key );

// (4) Non const version only
template< class K >
size_type erase( K&& x );
  • (1) 移除 pos 处的元素。如果 iteratorconst_iterator 是同一类型,则只提供一个重载。
  • (2) 移除范围 [ first; last ) 内的元素,该范围必须是 *this 中的有效范围。
  • (3) 移除(如果存在)键等于 key 的元素。
  • (4) 移除(如果存在)键与值 x 等效的元素。此重载仅在 Hash::is_transparentKeyEqual::is_transparent 有效且各自表示一个类型,并且 iteratorconst_iterator 都不能从 K 隐式转换时才参与重载解析。这假设此 Hash 可以用 KKey 类型调用,并且 KeyEqual 是透明的,这共同允许在不构造 Key 实例的情况下调用此函数。

未被擦除的元素的顺序保持不变。(这使得在遍历容器时可以擦除单个元素。)

失效

被擦除元素的引用迭代器失效。

其他迭代器和引用不受影响。

有效迭代器

迭代器 pos 必须有效且可解引用。因此,end() 迭代器(有效但不可解引用)不能用作 pos 的值。

参数

  • pos - 指向要移除元素的迭代器
  • first, last - 要移除的元素范围
  • key - 要移除元素的键值
  • x - 可以与键透明比较的任何类型的值,表示要移除的元素

返回值

  • (1-2) - 最后一个被移除元素之后的迭代器。
  • (3-4) - 移除的元素数量(01)。

复杂度

  • (1)
    平均情况:常数时间
    最坏情况:与容器大小成线性关系

  • (2)
    平均情况:std::distance(first, last) 成线性关系
    最坏情况:与容器大小成线性关系

  • (3)
    平均情况:c.count(key)
    最坏情况:与容器大小成线性关系

  • (4)
    平均情况:c.count(x)
    最坏情况:与容器大小成线性关系

异常

  • (1-2) (无)
  • (3-4) HashKeyEqual 对象抛出的任何异常。

备注

功能测试宏:__cpp_lib_associative_heterogeneous_erasure(用于重载 (4))。

示例

Main.cpp
#include <unordered_set>
#include <iostream>
int main()
{
std::unordered_set<int> c = { 1, 2, 3, 4, 1, 2, 3, 4 };

auto print = [&c] {
std::cout << "c = { ";
for(int n : c)
std::cout << n << ' ';
std::cout << "}\n";
};
print();

std::cout << "Erase all odd numbers:\n";
for(auto it = c.begin(); it != c.end(); ) {
if(*it % 2 != 0)
it = c.erase(it);
else
++it;
}
print();

std::cout << "Erase 1, erased count: " << c.erase(1) << '\n';
std::cout << "Erase 2, erased count: " << c.erase(2) << '\n';
std::cout << "Erase 2, erased count: " << c.erase(2) << '\n';
print();
}
可能输出
c = { 1 2 3 4 }
Erase all odd numbers:
c = { 2 4 }
Erase 1, erased count: 0
Erase 2, erased count: 1
Erase 2, erased count: 0
c = { 4 }
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。单击“编辑此页面”可查看此文档的所有更改。
悬停查看原始许可证。

std::unordered_set erase() 方法

// (1) Non const version only
iterator erase( iterator pos );
iterator erase( const_iterator pos );

// (2) Non const version only
iterator erase( const_iterator first, const_iterator last );

// (3) Non const version only
size_type erase( const Key& key );

// (4) Non const version only
template< class K >
size_type erase( K&& x );
  • (1) 移除 pos 处的元素。如果 iteratorconst_iterator 是同一类型,则只提供一个重载。
  • (2) 移除范围 [ first; last ) 内的元素,该范围必须是 *this 中的有效范围。
  • (3) 移除(如果存在)键等于 key 的元素。
  • (4) 移除(如果存在)键与值 x 等效的元素。此重载仅在 Hash::is_transparentKeyEqual::is_transparent 有效且各自表示一个类型,并且 iteratorconst_iterator 都不能从 K 隐式转换时才参与重载解析。这假设此 Hash 可以用 KKey 类型调用,并且 KeyEqual 是透明的,这共同允许在不构造 Key 实例的情况下调用此函数。

未被擦除的元素的顺序保持不变。(这使得在遍历容器时可以擦除单个元素。)

失效

被擦除元素的引用迭代器失效。

其他迭代器和引用不受影响。

有效迭代器

迭代器 pos 必须有效且可解引用。因此,end() 迭代器(有效但不可解引用)不能用作 pos 的值。

参数

  • pos - 指向要移除元素的迭代器
  • first, last - 要移除的元素范围
  • key - 要移除元素的键值
  • x - 可以与键透明比较的任何类型的值,表示要移除的元素

返回值

  • (1-2) - 最后一个被移除元素之后的迭代器。
  • (3-4) - 移除的元素数量(01)。

复杂度

  • (1)
    平均情况:常数时间
    最坏情况:与容器大小成线性关系

  • (2)
    平均情况:std::distance(first, last) 成线性关系
    最坏情况:与容器大小成线性关系

  • (3)
    平均情况:c.count(key)
    最坏情况:与容器大小成线性关系

  • (4)
    平均情况:c.count(x)
    最坏情况:与容器大小成线性关系

异常

  • (1-2) (无)
  • (3-4) HashKeyEqual 对象抛出的任何异常。

备注

功能测试宏:__cpp_lib_associative_heterogeneous_erasure(用于重载 (4))。

示例

Main.cpp
#include <unordered_set>
#include <iostream>
int main()
{
std::unordered_set<int> c = { 1, 2, 3, 4, 1, 2, 3, 4 };

auto print = [&c] {
std::cout << "c = { ";
for(int n : c)
std::cout << n << ' ';
std::cout << "}\n";
};
print();

std::cout << "Erase all odd numbers:\n";
for(auto it = c.begin(); it != c.end(); ) {
if(*it % 2 != 0)
it = c.erase(it);
else
++it;
}
print();

std::cout << "Erase 1, erased count: " << c.erase(1) << '\n';
std::cout << "Erase 2, erased count: " << c.erase(2) << '\n';
std::cout << "Erase 2, erased count: " << c.erase(2) << '\n';
print();
}
可能输出
c = { 1 2 3 4 }
Erase all odd numbers:
c = { 2 4 }
Erase 1, erased count: 0
Erase 2, erased count: 1
Erase 2, erased count: 0
c = { 4 }
本文源自此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。单击“编辑此页面”可查看此文档的所有更改。
悬停查看原始许可证。