Namespaces
Variants

std::set<Key,Compare,Allocator>:: find

From cppreference.net

iterator find ( const Key & key ) ;
(1) (constexpr since C++26)
const_iterator find ( const Key & key ) const ;
(2) (constexpr since C++26)
template < class K >
iterator find ( const K & x ) ;
(3) (C++14부터)
(constexpr since C++26)
template < class K >
const_iterator find ( const K & x ) const ;
(4) (C++14부터)
(constexpr since C++26)
1,2) 키와 동등한 요소를 찾습니다. key .
3,4) 키가 x 와 동등하게 비교되는 요소를 찾습니다.
이 오버로드는 Compare transparent 인 경우에만 오버로드 해결에 참여합니다. 이를 통해 Key 의 인스턴스를 생성하지 않고 이 함수를 호출할 수 있습니다.

목차

매개변수

key - 검색할 요소의 키 값
x - 키와 투명하게 비교될 수 있는 임의의 타입의 값

반환값

요청된 요소에 대한 반복자. 해당 요소를 찾지 못한 경우, 끝 지난(참조: end() ) 반복자가 반환됩니다.

복잡도

컨테이너 크기에 대해 로그 시간 복잡도를 가집니다.

참고 사항

기능 테스트 매크로 표준 기능
__cpp_lib_generic_associative_lookup 201304L (C++14) 연관 컨테이너 에서의 이종 비교 검색; ( 3,4 ) 오버로드

예제

#include <iostream>
#include <set>
struct LightKey
{
    int x;
};
struct FatKey
{
    int x;
    int data[1000]; // 무거운 데이터 블록
};
// 위에서 설명한 대로, 컨테이너는 이러한 오버로드에 접근하기 위해 std::less<> (또는 다른 투명한
// 비교자)를 사용해야 합니다. 여기에는 std::string과 std::string_view 간의 비교와 같은
// 표준 오버로드도 포함됩니다.
bool operator<(const FatKey& fk, const LightKey& lk) { return fk.x < lk.x; }
bool operator<(const LightKey& lk, const FatKey& fk) { return lk.x < fk.x; }
bool operator<(const FatKey& fk1, const FatKey& fk2) { return fk1.x < fk2.x; }
int main()
{
    // 간단한 비교 데모
    std::set<int> example{1, 2, 3, 4};
    if (auto search = example.find(2); search != example.end())
        std::cout << "Found " << (*search) << '\n';
    else
        std::cout << "Not found\n";
    // 투명한 비교 데모
    std::set<FatKey, std::less<>> example2{{1, {}}, {2, {}}, {3, {}}, {4, {}}};
    LightKey lk = {2};
    if (auto search = example2.find(lk); search != example2.end())
        std::cout << "Found " << search->x << '\n';
    else
        std::cout << "Not found\n";
}

출력:

Found 2
Found 2

참고 항목

특정 키와 일치하는 요소의 수를 반환합니다
(public member function)
특정 키와 일치하는 요소들의 범위를 반환합니다
(public member function)