Namespaces
Variants

std::flat_multiset<Key,Compare,KeyContainer>:: erase

From cppreference.net

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 transparent 이고, iterator const_iterator 모두 K 에서 암시적으로 변환되지 않는 경우에만 오버로드 해결에 참여합니다. 이를 통해 Key 인스턴스를 생성하지 않고 이 함수를 호출할 수 있습니다.

반복자 pos 는 역참조 가능해야 합니다. 따라서 end() 반복자(유효하지만 역참조할 수 없음)는 pos 의 값으로 사용할 수 없습니다.

목차

매개변수

pos - 제거할 요소에 대한 반복자
first, last - 제거할 요소들의 범위 를 정의하는 한 쌍의 반복자
key - 제거할 요소들의 키 값
x - 제거할 요소들을 나타내는 키와 투명하게 비교될 수 있는 임의 타입의 값

반환값

1-3) 제거된 마지막 요소 다음의 반복자.
4) 제거된 요소의 수.
5) 제거된 요소의 수.

예외

1-3) 아무것도 throw하지 않습니다.
4,5) Compare 객체에 의해 발생하는 모든 예외.

복잡도

Depends on underlying container. Typically linear.

예제

#include <flat_set>
#include <iostream>
int main()
{
    std::flat_multiset<int> c = {1, 2, 3, 4, 1, 2, 3, 4};
    auto print = [&c]
    {
        std::cout << "c = { ";
        for (int n : c)
            std::cout << n << ' ';
        std::cout << "}\n";
    };
    print();
    std::cout << "Erase all odd numbers:\n";
    for (auto it = c.begin(); it != c.end();)
    {
        if (*it % 2 != 0)
            it = c.erase(it);
        else
            ++it;
    }
    print();
    std::cout << "Erase 1, erased count: " << c.erase(1) << '\n';
    std::cout << "Erase 2, erased count: " << c.erase(2) << '\n';
    std::cout << "Erase 2, erased count: " << c.erase(2) << '\n';
    print();
}

출력:

c = { 1 1 2 2 3 3 4 4 }
Erase all odd numbers:
c = { 2 2 4 4 }
Erase 1, erased count: 0
Erase 2, erased count: 2
Erase 2, erased count: 0
c = { 4 4 }

참고 항목

내용을 지움
(public member function)