Namespaces
Variants

std::unordered_map<Key,T,Hash,KeyEqual,Allocator>:: unordered_map

From cppreference.net

(1)
unordered_map ( )
: unordered_map ( size_type ( /* unspecified */ ) ) { }
(C++11부터)
(C++20까지)
unordered_map ( ) ;
(C++20부터)
explicit unordered_map ( size_type bucket_count,

const Hash & hash = Hash ( ) ,
const key_equal & equal = key_equal ( ) ,

const Allocator & alloc = Allocator ( ) ) ;
(2) (C++11 이후)
unordered_map ( size_type bucket_count,

const Allocator & alloc )

: unordered_map ( bucket_count, Hash ( ) , key_equal ( ) , alloc ) { }
(3) (C++14 이후)
unordered_map ( size_type bucket_count,

const Hash & hash,
const Allocator & alloc )

: unordered_map ( bucket_count, hash, key_equal ( ) , alloc ) { }
(4) (C++14 이후)
explicit unordered_map ( const Allocator & alloc ) ;
(5) (C++11 이후)
template < class InputIt >

unordered_map ( InputIt first, InputIt last,
size_type bucket_count = /* unspecified */ ,
const Hash & hash = Hash ( ) ,
const key_equal & equal = key_equal ( ) ,

const Allocator & alloc = Allocator ( ) ) ;
(6) (C++11 이후)
template < class InputIt >

unordered_map ( InputIt first, InputIt last,
size_type bucket_count,
const Allocator & alloc )
: unordered_map ( first, last,

bucket_count, Hash ( ) , key_equal ( ) , alloc ) { }
(7) (C++14 이후)
template < class InputIt >

unordered_map ( InputIt first, InputIt last,
size_type bucket_count,
const Hash & hash,
const Allocator & alloc )
: unordered_map ( first, last,

bucket_count, hash, key_equal ( ) , alloc ) { }
(8) (C++14부터)
unordered_map ( const unordered_map & other ) ;
(9) (C++11 이후)
unordered_map ( const unordered_map & other, const Allocator & alloc ) ;
(10) (C++11 이후)
unordered_map ( unordered_map && other ) ;
(11) (C++11 이후)
unordered_map ( unordered_map && other, const Allocator & alloc ) ;
(12) (C++11 이후)
unordered_map ( std:: initializer_list < value_type > init,

size_type bucket_count = /* 지정되지 않음 */ ,
const Hash & hash = Hash ( ) ,
const key_equal & equal = key_equal ( ) ,

const Allocator & alloc = Allocator ( ) ) ;
(13) (C++11 이후)
unordered_map ( std:: initializer_list < value_type > init,

size_type bucket_count,
const Allocator & alloc )
: unordered_map ( init, bucket_count,

Hash ( ) , key_equal ( ) , alloc ) { }
(14) (C++14부터)
unordered_map ( std:: initializer_list < value_type > init,

size_type bucket_count,
const Hash & hash,
const Allocator & alloc )
: unordered_map ( init, bucket_count,

hash, key_equal ( ) , alloc ) { }
(15) (C++14 이후)
template < container-compatible-range < value_type > R >

unordered_map ( std:: from_range_t , R && rg,
size_type bucket_count = /* 설명 참조 */ ,
const Hash & hash = Hash ( ) ,
const key_equal & equal = key_equal ( ) ,

const Allocator & alloc = Allocator ( ) ) ;
(16) (C++23부터)
template < container-compatible-range < value_type > R >

unordered_map ( std:: from_range_t , R && rg,
size_type bucket_count,
const Allocator & alloc )
: unordered_map ( std:: from_range , std:: forward < R > ( rg ) ,

bucket_count, Hash ( ) , key_equal ( ) , alloc ) { }
(17) (C++23부터)
template < container-compatible-range < value_type > R >

unordered_map ( std:: from_range_t , R && rg,
size_type bucket_count,
const Hash & hash,
const Alloc & alloc )
: unordered_map ( std:: from_range , std:: forward < R > ( rg ) ,

bucket_count, hash, key_equal ( ) , alloc ) { }
(18) (C++23부터)

다양한 데이터 소스로부터 새로운 컨테이너를 생성합니다. 선택적으로 사용자가 제공한 bucket_count 를 생성할 최소 버킷 수로 사용하고, hash 를 해시 함수로, equal 를 키 비교 함수로, alloc 를 할당자로 사용합니다.

1-5) 빈 컨테이너를 생성합니다. max_load_factor() 1.0 으로 설정합니다. 기본 생성자의 경우 버킷 수는 명시되지 않습니다.
6-8) 범위 [ first , last ) 의 내용으로 컨테이너를 생성합니다. max_load_factor() 1.0 으로 설정합니다. 범위 내에 동등한 키를 가진 여러 요소가 있는 경우, 어떤 요소가 삽입될지 명시되지 않습니다 ( LWG2844 보류 중).
9,10) 복사 생성자. 컨테이너를 other 의 내용 복사본으로 구성하며, 로드 팩터, predicate 및 해시 함수도 복사합니다. alloc 이 제공되지 않으면, allocator는 std:: allocator_traits < allocator_type > :: select_on_container_copy_construction ( other. get_allocator ( ) ) 를 호출하여 얻습니다.

템플릿 매개변수 Allocator 클래스 템플릿 인수 추론 에서 사용될 때 첫 번째 인수로부터만 추론됩니다.

(C++23 이후)
11,12) 이동 생성자 . 이동 의미론을 사용하여 other 의 내용으로 컨테이너를 생성합니다. alloc 이 제공되지 않으면, other 에 속한 할당자로부터 이동 생성으로 할당자를 획득합니다.

템플릿 매개변수 Allocator 클래스 템플릿 인수 추론 에서 사용될 때 첫 번째 인수에서만 추론됩니다.

(C++23 이후)
13-15) 초기화자 리스트 생성자 . 초기화자 리스트 init 의 내용으로 컨테이너를 생성합니다. unordered_map ( init. begin ( ) , init. end ( ) ) 와 동일합니다.
16-18) 범위 rg 의 내용으로 컨테이너를 생성합니다. 범위 내에서 비교 시 동등한 키를 가진 여러 요소가 있는 경우, 어떤 요소가 삽입될지 지정되지 않습니다 (보류 중인 LWG2844 ).

목차

매개변수

alloc - 이 컨테이너의 모든 메모리 할당에 사용할 할당자
bucket_count - 초기화 시 사용할 최소 버킷 수. 지정되지 않으면 명시되지 않은 기본값이 사용됨
hash - 사용할 해시 함수
equal - 이 컨테이너의 모든 키 비교에 사용할 비교 함수
first, last - 복사할 요소들의 소스 범위 를 정의하는 반복자 쌍
rg - 컨테이너 호환 범위 , 즉 요소들이 value_type 으로 변환 가능한 input_range
other - 컨테이너의 요소들을 초기화하는 데 사용할 소스로 사용할 다른 컨테이너
init - 컨테이너의 요소들을 초기화하는 데 사용할 초기화자 리스트
타입 요구사항
-
InputIt LegacyInputIterator 요구사항을 충족해야 함

복잡도

1-5) 상수.
6-8) 평균 케이스 선형 (즉 O(N) , 여기서 N std:: distance ( first, last ) ), 최악 케이스 2차, 즉 O(N 2 ) .
9,10) other 의 크기에 선형적입니다.
11,12) 상수. 만약 alloc 이 주어지고 alloc ! = other. get_allocator ( ) 인 경우, 선형입니다.
13-15) 평균 케이스 O(N) ( N std:: size ( init ) ), 최악의 케이스 O(N 2 ) .
16-18) 평균 케이스 O(N) ( N ranges:: distance ( rg ) ), 최악의 케이스 O(N 2 ) .

예외

Allocator::allocate 에 대한 호출은 예외를 발생시킬 수 있습니다.

참고 사항

After container move construction (overload ( 11,12 ) ), references, pointers, and iterators (other than the end iterator) to other remain valid, but refer to elements that are now in * this . The current standard makes this guarantee via the blanket statement in [container.reqmts]/67 , and a more direct guarantee is under consideration via LWG 이슈 2321 .

C++23까지 공식적으로 요구되지는 않지만, 일부 구현에서는 이미 이전 모드에서 템플릿 매개변수 Allocator 비추론 문맥 에 배치한 경우가 있습니다.

기능 테스트 매크로 표준 기능
__cpp_lib_containers_ranges 202202L (C++23) 범위 인식 생성 및 삽입; 오버로드 ( 16-18 )

예제

#include <bitset>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
struct Key
{
    std::string first;
    std::string second;
};
struct KeyHash
{
    std::size_t operator()(const Key& k) const
    {
        return std::hash<std::string>()(k.first) ^
            (std::hash<std::string>()(k.second) << 1);
    }
};
struct KeyEqual
{
    bool operator()(const Key& lhs, const Key& rhs) const
    {
        return lhs.first == rhs.first && lhs.second == rhs.second;
    }
};
struct Foo
{
    Foo(int val_) : val(val_) {}
    int val;
    bool operator==(const Foo &rhs) const { return val == rhs.val; }
};
template<>
struct std::hash<Foo>
{
    std::size_t operator()(const Foo &f) const
    {
        return std::hash<int>{}(f.val);
    }
};
int main()
{
    // 기본 생성자: 빈 맵
    std::unordered_map<std::string, std::string> m1;
    // list constructor
    std::unordered_map<int, std::string> m2 =
    {
        {1, "foo"},
        {3, "bar"},
        {2, "baz"}
    };
    // 복사 생성자
    std::unordered_map<int, std::string> m3 = m2;
    // 이동 생성자
    std::unordered_map<int, std::string> m4 = std::move(m2);
    // range constructor
    std::vector<std::pair<std::bitset<8>, int>> v = {{0x12, 1}, {0x01,-1}};
    std::unordered_map<std::bitset<8>, double> m5(v.begin(), v.end());
    // 사용자 정의 Key 타입을 위한 생성자 옵션 1
    // KeyHash 및 KeyEqual 구조체를 정의하고 템플릿에서 사용
    std::unordered_map<Key, std::string, KeyHash, KeyEqual> m6 =
    {
        {{"존", "Doe"}, "example"},
        {{"Mary", "Sue"}, "다른"}
    };
    // 사용자 정의 Key 타입을 사용하는 생성자를 위한 옵션 2.
    // 클래스/구조체에 대한 const == 연산자를 정의하고 std::hash를 특수화합니다
    // std 네임스페이스 내의 구조체
    std::unordered_map<Foo, std::string> m7 =
    {
        {Foo(1), "하나"}, {2, "Two"}, {3, "Three"}
    };
    // Option 3: Use lambdas
    // 초기 버킷 수는 생성자에 전달되어야 함
    struct Goo { int val; };
    auto hash = [](const Goo &g){ return std::hash<int>{}(g.val); };
    auto comp = [](const Goo &l, const Goo &r){ return l.val == r.val; };
    std::unordered_map<Goo, double, decltype(hash), decltype(comp)> m8(10, hash, comp);
}

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 2193 C++11 기본 생성자 ( 1 ) 가 explicit였음 non-explicit로 변경됨
LWG 2230 C++11 오버로드 ( 13 ) 의 의미론이 명시되지 않았음 명시됨

참고 항목

컨테이너에 값을 할당합니다
(public member function)