다음을 통해 공유


set::find

지정된 키와 같은 키를 포함하는 집합 내 요소의 위치를 가리키는 반복기를 반환합니다.

iterator find(const Key& key);  const_iterator find(const Key& key) const; 

매개 변수

  • key
    검색 중인 집합에서 요소의 정렬 키와 일치하는지 확인할 키 값입니다.

반환 값

지정된 키를 포함하는 요소의 위치 또는 해당 키와 일치하는 항목이 없는 경우 집합의 마지막 요소(set::end()) 다음 위치를 가리키는 반복기입니다.

설명

멤버 함수는 보다 작음 비교 가능 관계를 기반으로 순서를 적용하는 이진 조건자에서 해당 키가 key 인수와 같은 집합 내 요소를 가리키는 반복기를 반환합니다.

find의 반환 값이 const_iterator에 할당되는 경우 집합 개체를 수정할 수 없습니다. find의 반환 값이 iterator에 할당되는 경우에는 집합 개체를 수정할 수 있습니다.

예제

// compile with: /EHsc /W4 /MTd
#include <set>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

template <typename T> void print_elem(const T& t) {
    cout << "(" << t << ") ";
}

template <typename T> void print_collection(const T& t) {
    cout << t.size() << " elements: ";

    for (const auto& p : t) {
        print_elem(p);
    }
    cout << endl;
}

template <typename C, class T> void findit(const C& c, T val) {
    cout << "Trying find() on value " << val << endl;
    auto result = c.find(val);
    if (result != c.end()) {
        cout << "Element found: "; print_elem(*result); cout << endl;
    } else {
        cout << "Element not found." << endl;
    }
}

int main()
{
    set<int> s1({ 40, 45 });
    cout << "The starting set s1 is: " << endl;
    print_collection(s1);

    vector<int> v;
    v.push_back(43);
    v.push_back(41);
    v.push_back(46);
    v.push_back(42);
    v.push_back(44);
    v.push_back(44); // attempt a duplicate

    cout << "Inserting the following vector data into s1: " << endl;
    print_collection(v);

    s1.insert(v.begin(), v.end());

    cout << "The modified set s1 is: " << endl;
    print_collection(s1);
    cout << endl;
    findit(s1, 45);
    findit(s1, 6);
}

출력

  

요구 사항

헤더: <set>

네임스페이스: std

참고 항목

참조

set 클래스

표준 템플릿 라이브러리