std::flat_multimap<Key,T,Compare,KeyContainer,MappedContainer>:: erase
From cppreference.net
<
cpp
|
container
|
flat multimap
|
iterator erase
(
iterator pos
)
;
|
(1) |
(自 C++23 起)
(自 C++26 起为 constexpr) |
|
iterator erase
(
const_iterator pos
)
;
|
(2) |
(自 C++23 起)
(自 C++26 起为 constexpr) |
|
iterator erase
(
const_iterator first, const_iterator last
)
;
|
(3) |
(自 C++23 起)
(自 C++26 起为 constexpr) |
|
size_type erase
(
const
Key
&
key
)
;
|
(4) |
(自 C++23 起)
(自 C++26 起为 constexpr) |
|
template
<
class
K
>
size_type erase ( K && x ) ; |
(5) |
(自 C++23 起)
(自 C++26 起为 constexpr) |
从容其中移除指定的元素。保留剩余等价元素的原有顺序。
1,2)
移除位于
pos
处的元素。
3)
移除范围
[
first
,
last
)
中的元素,该范围必须是
*
this
中的有效范围。
4)
移除所有键值等价于
key
的元素。
5)
移除所有键值与
x
等价的元素。
仅当
Compare
满足
透明性要求
,且
iterator
与
const_iterator
均不能从
K
隐式转换时,此重载才会参与重载决议。该特性允许在不构造
Key
实例的情况下调用此函数。
| 关于迭代器失效的信息复制自 此处 |
迭代器 pos 必须是可解引用的。因此 end() 迭代器(虽然有效但不可解引用)不能用作 pos 的值。
目录 |
参数
| pos | - | 指向待移除元素的迭代器 |
| first, last | - | 定义待移除元素 范围 的迭代器对 |
| key | - | 待移除元素的键值 |
| x | - | 可与键值进行透明比较的任意类型值,用于指定待移除元素 |
返回值
1-3)
指向被移除元素之后位置的迭代器。
4)
被移除的元素数量。
5)
被移除的元素数量。
异常
1-3)
不抛出任何异常。
4,5)
由
Compare
对象抛出的任何异常。
复杂度
Depends on underlying containers. Typically linear.|
本节内容不完整
原因:需要修订 |
示例
运行此代码
#include <flat_map> #include <iostream> int main() { std::flat_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 != 0) it = c.erase(it); else ++it; } for (auto& p : c) std::cout << p.second << ' '; std::cout << '\n'; }
输出:
two four six
另请参阅
|
清空内容
(公开成员函数) |