다음을 통해 공유


set::erase

지정 된 위치에서 요소 또는 요소 범위를 제거하거나 지정된 된 키와 일치하는 요소를 제거합니다.

iterator erase(
   const_iterator Where
);
iterator erase(
   const_iterator First,
   const_iterator Last
);
size_type erase(
   const key_type& Key
);

매개 변수

  • Where
    제거할 요소의 위치입니다.

  • First
    제거할 최초 요소의 위치입니다.

  • Last
    제거할 마지막 요소의 바로 다음 위치입니다.

  • Key
    제거할 {키, 값} 쌍의 키

반환 값

첫 번째 두 멤버 함수에서, 양방향 반복기는 지정하는 모든 요소가 제거된 다음 남은 첫 요소나, 이러한 요소가 없는 경우 집합의 끝 요소를 지정합니다.

세 번째 멤버 함수에서, 집합에서 제거된 요소 수를 반환합니다.

예제

// set_erase.cpp
// compile with: /EHsc
#include <set>
#include <string>
#include <iostream>
#include <iterator> // next() and prev() helper functions

using namespace std;

using myset = set<string>;

void printset(const myset& s) {
    for (const auto& iter : s) {
        cout << " [" << iter << "]";
    }
    cout << endl << "size() == " << s.size() << endl << endl;
}

int main()
{
    myset s1;

    // Fill in some data to test with, one at a time
    s1.insert("Bob");
    s1.insert("Robert");
    s1.insert("Bert");
    s1.insert("Rob");
    s1.insert("Bobby");

    cout << "Starting data of set s1 is:" << endl;
    printset(s1);
    // The 1st member function removes an element at a given position
    s1.erase(next(s1.begin()));
    cout << "After the 2nd element is deleted, the set s1 is:" << endl;
    printset(s1);

    // Fill in some data to test with, one at a time, using an intializer list
    myset s2{ "meow", "hiss", "purr", "growl", "yowl" };

    cout << "Starting data of set s2 is:" << endl;
    printset(s2);
    // The 2nd member function removes elements
    // in the range [First, Last)
    s2.erase(next(s2.begin()), prev(s2.end()));
    cout << "After the middle elements are deleted, the set s2 is:" << endl;
    printset(s2);

    myset s3;

    // Fill in some data to test with, one at a time, using emplace
    s3.emplace("C");
    s3.emplace("C#");
    s3.emplace("D");
    s3.emplace("D#");
    s3.emplace("E");
    s3.emplace("E#");
    s3.emplace("F");
    s3.emplace("F#");
    s3.emplace("G");
    s3.emplace("G#");
    s3.emplace("A");
    s3.emplace("A#");
    s3.emplace("B");

    cout << "Starting data of set s3 is:" << endl;
    printset(s3);
    // The 3rd member function removes elements with a given Key
    myset::size_type count = s3.erase("E#");
    // The 3rd member function also returns the number of elements removed
    cout << "The number of elements removed from s3 is: " << count << "." << endl;
    cout << "After the element with a key of \"E#\" is deleted, the set s3 is:" << endl;
    printset(s3);
}

Output

Starting data of set s1 is:
 [Bert] [Bob] [Bobby] [Rob] [Robert]
size() == 5

After the 2nd element is deleted, the set s1 is:
 [Bert] [Bobby] [Rob] [Robert]
size() == 4

Starting data of set s2 is:
 [growl] [hiss] [meow] [purr] [yowl]
size() == 5

After the middle elements are deleted, the set s2 is:
 [growl] [yowl]
size() == 2

Starting data of set s3 is:
 [A] [A#] [B] [C] [C#] [D] [D#] [E] [E#] [F] [F#] [G] [G#]
size() == 13

The number of elements removed from s3 is: 1.
After the element with a key of "E#" is deleted, the set s3 is:
 [A] [A#] [B] [C] [C#] [D] [D#] [E] [F] [F#] [G] [G#]
size() == 12

요구 사항

헤더: <설정>

네임스페이스: std

참고 항목

참조

<set>

set 클래스

set::clear

표준 템플릿 라이브러리