名稱空間
變體
操作

std::erase_if (std::flat_map)

來自 cppreference.com
< cpp‎ | 容器‎ | flat map
 
 
 
 
在標頭檔案 <flat_map> 中定義
template< class Key, class T, class Compare, class KeyContainer, class MappedContainer,

          class Pred >
std::flat_map<Key, T, Compare, KeyContainer, MappedContainer>::size_type
    erase_if( std::flat_map<Key, T, Compare, KeyContainer, MappedContainer>& c,

              Pred pred );
(C++23 起)

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

如果表示式 bool(pred(std::pair<const Key&, const T&>(e)))true,則謂詞 pred 滿足,其中 ec 中的某個元素。

KeyT 必須是 可移動賦值 (MoveAssignable)。否則,行為未定義。

目錄

[編輯] 引數

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

[編輯] 返回值

被刪除元素的數量。

[編輯] 複雜度

謂詞 pred 的精確呼叫次數為 c.size()

異常

如果 erase_if 丟擲異常,則 c 保持有效但未指定(可能為空)狀態。

注意

該演算法是穩定的,即未被刪除的元素的順序保持不變。

[編輯] 示例

#include <iostream>
#include <flat_map>
 
void println(auto rem, auto const& container)
{
    std::cout << rem << '{';
    for (char sep[]{0, ' ', 0}; const auto& [key, value] : container)
        std::cout << sep << '{' << key << ", " << value << '}', *sep = ',';
    std::cout << "}\n";
}
 
int main()
{
    std::flat_map<int, char> data
    {
        {1, 'a'}, {2, 'b'}, {3, 'c'}, {4, 'd'},
        {5, 'e'}, {4, 'f'}, {5, 'g'}, {5, 'g'},
    };
    println("Original:\n", data);
 
    const auto count = std::erase_if(data, [](const auto& item)
    {
        auto const& [key, value] = item;
        return (key & 1) == 1;
    });
 
    println("Erase items with odd keys:\n", data);
    std::cout << count << " items removed.\n";
}

輸出

Original:
{{1, a}, {2, b}, {3, c}, {4, d}, {5, e}}
Erase items with odd keys:
{{2, b}, {4, d}}
3 items removed.

[編輯] 另請參閱

移除滿足特定標準的元素
(函式模板) [編輯]
(C++20 起)(C++20 起)
移除滿足特定標準的元素
(演算法函式物件)[編輯]