std::unordered_set<Key,Hash,KeyEqual,Allocator>:: find
From cppreference.net
<
cpp
|
container
|
unordered set
|
iterator find
(
const
Key
&
key
)
;
|
(1) |
(C++11부터)
(C++26부터 constexpr) |
|
const_iterator find
(
const
Key
&
key
)
const
;
|
(2) |
(C++11부터)
(C++26부터 constexpr) |
|
template
<
class
K
>
iterator find ( const K & x ) ; |
(3) |
(C++20부터)
(C++26부터 constexpr) |
|
template
<
class
K
>
const_iterator find ( const K & x ) const ; |
(4) |
(C++20부터)
(C++26부터 constexpr) |
1,2)
키와 동등한 요소를 찾습니다.
key
.
3,4)
키가
x
와 동등하게 비교되는 요소를 찾습니다.
이 오버로드는
Hash
와
KeyEqual
가 모두
transparent
인 경우에만 오버로드 해결에 참여합니다. 이는 해당
Hash
가
K
와
Key
타입 모두로 호출 가능하고,
KeyEqual
가 transparent임을 가정하며, 이를 통해
Key
의 인스턴스를 생성하지 않고 이 함수를 호출할 수 있습니다.
목차 |
매개변수
| key | - | 검색할 요소의 키 값 |
| x | - | 키와 투명하게 비교될 수 있는 임의의 타입의 값 |
반환값
요청된 요소에 대한 반복자. 해당 요소를 찾지 못한 경우, 끝 지난(참조: end() ) 반복자가 반환됩니다.
복잡도
평균적으로는 상수 시간, 최악의 경우 컨테이너 크기에 선형적으로 증가합니다.
참고 사항
| Feature-test 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_generic_unordered_lookup
|
201811L
|
(C++20) | 비정렬 연관 컨테이너 에서의 이종 비교 검색; ( 3,4 ) 오버로드 |
예제
이 코드 실행
#include <cstddef> #include <functional> #include <iostream> #include <source_location> #include <string> #include <string_view> #include <unordered_set> using namespace std::literals; namespace logger { bool enabled{false}; } inline void who(const std::source_location sloc = std::source_location::current()) { if (logger::enabled) std::cout << sloc.function_name() << '\n'; } struct string_hash // C++20's transparent hashing { using hash_type = std::hash<std::string_view>; using is_transparent = void; std::size_t operator()(const char* str) const { who(); return hash_type{}(str); } std::size_t operator()(std::string_view str) const { who(); return hash_type{}(str); } std::size_t operator()(const std::string& str) const { who(); return hash_type{}(str); } }; int main() { std::unordered_set<int> example{1, 2, -10}; std::cout << "Simple comparison demo:\n" << std::boolalpha; if (auto search = example.find(2); search != example.end()) std::cout << "Found " << *search << '\n'; else std::cout << "Not found\n"; std::unordered_set<std::string, string_hash, std::equal_to<>> set{"one"s, "two"s}; logger::enabled = true; std::cout << "Heterogeneous lookup for unordered containers (transparent hashing):\n" << (set.find("one") != set.end()) << '\n' << (set.find("one"s) != set.end()) << '\n' << (set.find("one"sv) != set.end()) << '\n'; }
가능한 출력:
Simple comparison demo: Found 2 Heterogeneous lookup for unordered containers (transparent hashing): std::size_t string_hash::operator()(const char*) const true std::size_t string_hash::operator()(const std::string&) const true std::size_t string_hash::operator()(std::string_view) const true
참고 항목
|
특정 키와 일치하는 요소의 수를 반환합니다
(public member function) |
|
|
특정 키와 일치하는 요소들의 범위를 반환합니다
(public member function) |