std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>:: at
From cppreference.net
|
T
&
at
(
const
Key
&
key
)
;
|
(1) | (C++23부터) |
|
const
T
&
at
(
const
Key
&
key
)
const
;
|
(2) | (C++23부터) |
|
template
<
class
K
>
T & at ( const K & x ) ; |
(3) | (C++23부터) |
|
template
<
class
K
>
const T & at ( const K & x ) const ; |
(4) | (C++23부터) |
지정된 키를 가진 요소의 매핑된 값에 대한 참조를 반환합니다. 해당 요소가 존재하지 않을 경우, std::out_of_range 타입의 예외가 발생합니다.
1,2)
키는
key
와 동등합니다.
3,4)
키가 값
x
와
동등
하게 비교됩니다. 매핑된 값에 대한 참조는
this
-
>
find
(
x
)
-
>
second
표현식으로 얻은 것처럼 획득됩니다.
표현식
this
-
>
find
(
x
)
는 올바른 형태를 가져야 하며 명확하게 정의된 동작을 가져야 합니다. 그렇지 않으면 동작은 정의되지 않습니다.
목차 |
매개변수
| key | - | 검색할 요소의 키 |
| x | - | 키와 투명하게 비교될 수 있는 임의 타입의 값 |
반환값
요청된 요소의 매핑된 값에 대한 참조입니다.
예외
복잡도
컨테이너 크기에 대해 로그 시간 복잡도를 가집니다.
예제
이 코드 실행
#include <cassert> #include <iostream> #include <flat_map> struct LightKey { int o; }; struct HeavyKey { int o[1000]; }; // 컨테이너는 오버로드 (3,4)에 접근하기 위해 std::less<> (또는 다른 투명 비교자)를 사용해야 합니다. // 여기에는 std::string과 std::string_view 간의 비교와 같은 표준 오버로드가 포함됩니다. bool operator<(const HeavyKey& x, const LightKey& y) { return x.o[0] < y.o; } bool operator<(const LightKey& x, const HeavyKey& y) { return x.o < y.o[0]; } bool operator<(const HeavyKey& x, const HeavyKey& y) { return x.o[0] < y.o[0]; } int main() { std::flat_map<int, char> map{{1, 'a'}, {2, 'b'}}; assert(map.at(1) == 'a'); assert(map.at(2) == 'b'); try { map.at(13); } catch(const std::out_of_range& ex) { std::cout << "1) out_of_range::what(): " << ex.what() << '\n'; } #ifdef __cpp_lib_associative_heterogeneous_insertion // 투명 비교 데모. std::flat_map<HeavyKey, char, std::less<>> map2{{{1}, 'a'}, {{2}, 'b'}}; assert(map2.at(LightKey{1}) == 'a'); assert(map2.at(LightKey{2}) == 'b'); try { map2.at(LightKey{13}); } catch(const std::out_of_range& ex) { std::cout << "2) out_of_range::what(): " << ex.what() << '\n'; } #endif }
가능한 출력:
1) out_of_range::what(): map::at: key not found 2) out_of_range::what(): map::at: key not found
참고 항목
|
지정된 요소에 접근하거나 삽입
(public member function) |
|
|
특정 키를 가진 요소를 찾음
(public member function) |