std::flat_multimap<Key,T,Compare,KeyContainer,MappedContainer>:: erase
From zh.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 的值。
目录参数
返回值
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> map =
{
{1, "one"}, {2, "two"}, {3, "three"},
{4, "four"}, {5, "five"}, {6, "six"}
};
// erase all odd keys from map
for (auto it = map.begin(); it != map.end();)
{
if (it->first % 2 != 0)
it = map.erase(it);
else
++it;
}
for (const auto& pair : map)
std::cout << pair.second << ' ';
std::cout << '\n';
}
输出: two four six
另请参阅
|