Namespaces
Variants

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

From cppreference.net

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

목차

매개변수

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

반환값

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

복잡도

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

예제

#include <iostream>
#include <flat_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::flat_multiset<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::flat_multiset<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)