名稱空間
變體
操作

std::erase_if (std::multiset)

來自 cppreference.com
< cpp‎ | 容器‎ | multiset
 
 
 
 
在標頭檔案 <set> 中定義
template< class Key, class Compare, class Alloc,

          class Pred >
std::multiset<Key, Compare, Alloc>::size_type
    erase_if( std::multiset<Key, Compare, Alloc>& c,

              Pred pred );
(C++20 起)

c 中擦除所有滿足謂詞 pred 的元素。

等價於

auto old_size = c.size();
for (auto first = c.begin(), last = c.end(); first != last;)
{
    if (pred(*first))
        first = c.erase(first);
    else
        ++first;
}
return old_size - c.size();

目錄

[編輯] 引數

c - 要從中刪除元素的容器
pred - 如果元素應被擦除,則返回 true 的謂詞

[編輯] 返回值

被刪除元素的數量。

[編輯] 複雜度

線性。

[編輯] 示例

#include <iostream>
#include <set>
 
void println(auto rem, auto const& container)
{
    std::cout << rem << '{';
    for (char sep[]{0, ' ', 0}; const auto& item : container)
        std::cout << sep << item, *sep = ',';
    std::cout << "}\n";
}
 
int main()
{
    std::multiset data{3, 3, 4, 5, 5, 6, 6, 7, 2, 1, 0};
    println("Original:\n", data);
 
    auto divisible_by_3 = [](auto const& x) { return (x % 3) == 0; };
 
    const auto count = std::erase_if(data, divisible_by_3);
 
    println("Erase all items divisible by 3:\n", data);
    std::cout << count << " items erased.\n";
}

輸出

Original:
{0, 1, 2, 3, 3, 4, 5, 5, 6, 6, 7}
Erase all items divisible by 3:
{1, 2, 4, 5, 5, 7}
5 items erased.

[編輯] 另請參閱

移除滿足特定標準的元素
(function template) [edit]
移除滿足特定標準的元素
(algorithm function object)[edit]