flat_set_find
flat_set_find (std::flat_set::find — 키로 원소 찾기)
std::flat_set에서 특정 키와 동등한 원소를 찾아 이터레이터로 반환하는 멤버 함수예요. 투명 비교자(Compare::is_transparent)를 쓰면 Key 인스턴스를 만들지 않고 값을 기준으로 검색할 수 있어요.
출처: cppreference
본문
시그니처는 다음과 같아요.
iterator find( const Key& key ); // (1) (since C++23)
const_iterator find( const Key& key ) const; // (2) (since C++23)
template< class K >
iterator find( const K& x ); // (3) (since C++23)
template< class K >
const_iterator find( const K& x ) const; // (4) (since C++23)
(1,2) key와 동등한 키를 가진 원소를 찾아요.
(3,4) x와 동등(equivalent) 하게 비교되는 키를 가진 원소를 찾아요. 이 오버로드는 한정 식별자 Compare::is_transparent가 유효하고 타입을 나타낼 때만 오버로드 해석에 참여해요. Key 인스턴스를 만들지 않고도 이 함수를 호출할 수 있게 해 주는 오버로드예요.
매개변수
key: 찾을 원소의 키 값x: 키와 투명하게 비교할 수 있는 어떤 타입의 값
반환값
요청한 원소를 가리키는 이터레이터. 그런 원소가 없으면 past-the-end(end() 참고) 이터레이터를 반환해요.
복잡도
컨테이너 크기에 대해 로그(logarithmic)예요.
예제
#include <iostream>
#include <flat_set>
int main()
{
std::flat_set<int> example{1, 2, 3, 4};
auto it = example.find(3);
if (it != example.end())
std::cout << "Found: " << *it << '\n';
it = example.find(10);
if (it == example.end())
std::cout << "Not found\n";
}