Namespaces
Variants

std:: erase_if (std::flat_map)

From cppreference.net

헤더 파일에 정의됨 <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++26부터 constexpr)

pred 술어를 만족하는 모든 요소를 c 에서 삭제합니다.

술어 pred 는 표현식 bool ( pred ( std:: pair < const Key & , const T & > ( e ) ) ) true 일 때 만족되며, 여기서 e c 내의 어떤 요소입니다.

만약 Key 또는 T MoveAssignable 이 아닌 경우, 동작은 정의되지 않습니다.

목차

매개변수

c - 삭제할 컨테이너 어댑터
pred - 요소가 삭제되어야 할 경우 true 를 반환하는 조건자

반환값

삭제된 요소의 수.

복잡도

정확히 c. size ( ) 번의 predicate pred 적용.

예외

만약 erase_if 가 예외를 발생시키면, c 는 유효하지만 지정되지 않은(비어 있을 수도 있는) 상태로 유지됩니다.

참고 사항

알고리즘은 안정적입니다. 즉, 삭제되지 않은 요소들의 순서가 변경되지 않습니다.

예제

#include <iostream>
#include <flat_map>
void println(auto rem, const auto& 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)
    {
        const auto& [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.

참고 항목

특정 조건을 만족하는 요소들을 제거함
(함수 템플릿)
특정 조건을 만족하는 요소들을 제거함
(알고리즘 함수 객체)