std::unordered_map<Key,T,Hash,KeyEqual,Allocator>:: find
From cppreference.net
<
cpp
|
container
|
unordered map
|
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 | - | 키와 투명하게 비교될 수 있는 임의의 타입의 값 |
반환값
요청된 요소에 대한 반복자. 해당 요소를 찾지 못한 경우, 끝 지난(past-the-end) (참조: end() ) 반복자가 반환됩니다.
복잡도
평균적으로는 상수 시간, 최악의 경우 컨테이너 크기에 선형적으로 증가합니다.
참고 사항
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_generic_unordered_lookup
|
201811L
|
(C++20) | 비정렬 연관 컨테이너 에서의 이종 비교 검색; ( 3,4 ) 오버로드 |
예제
이 코드 실행
#include <cstddef> #include <functional> #include <iostream> #include <string> #include <string_view> #include <unordered_map> using namespace std::literals; struct string_hash { using hash_type = std::hash<std::string_view>; using is_transparent = void; std::size_t operator()(const char* str) const { return hash_type{}(str); } std::size_t operator()(std::string_view str) const { return hash_type{}(str); } std::size_t operator()(const std::string& str) const { return hash_type{}(str); } }; int main() { // 간단한 비교 데모 std::unordered_map<int, char> example{{1, 'a'}, {2, 'b'}}; if (auto search = example.find(2); search != example.end()) std::cout << "Found " << search->first << ' ' << search->second << '\n'; else std::cout << "Not found\n"; // C++20 데모: 비정형 컨테이너를 위한 이종 검색 (투명 해싱) std::unordered_map<std::string, size_t, string_hash, std::equal_to<>> map{{"one"s, 1}}; std::cout << std::boolalpha << (map.find("one") != map.end()) << '\n' << (map.find("one"s) != map.end()) << '\n' << (map.find("one"sv) != map.end()) << '\n'; }
출력:
Found 2 b true true true
참고 항목
|
경계 검사와 함께 지정된 요소에 접근
(public member function) |
|
|
지정된 요소에 접근 또는 삽입
(public member function) |
|
|
특정 키와 일치하는 요소의 수를 반환
(public member function) |
|
|
특정 키와 일치하는 요소들의 범위를 반환
(public member function) |