Freigeben über


unordered_map::unordered_map

Constructs a container object.

unordered_map(
    const unordered_map& right);
explicit unordered_map(
    size_type nbuckets = N0,
    const Hash& hfn = Hash(),
    const Pred& comp = Pred(),
    const Alloc& al = Alloc());
template<class InIt>
    unordered_map(
    InIt first, InIt last,
    size_type nbuckets = N0,
    const Hash& hfn = Hash(),
    const Pred& comp = Pred(),
    const Alloc& al = Alloc());
unordered_map(
    unordered_map&& right);

Parameters

Parameter

Description

InIt

The iterator type.

al

The allocator object to store.

comp

The comparison function object to store.

hfn

The hash function object to store.

nbuckets

The minimum number of buckets.

right

The container to copy.

Remarks

The first constructor specifies a copy of the sequence controlled by right. The second constructor specifies an empty controlled sequence. The third constructor inserts the sequence of element values [first, last). The fourth constructor specifies a copy of the sequence by moving right.

All constructors also initialize several stored values. For the copy constructor, the values are obtained from right. Otherwise:

the minimum number of buckets is the argument nbuckets, if present; otherwise it is a default value described here as the implementation-defined value N0.

the hash function object is the argument hfn, if present; otherwise it is Hash().

the comparison function object is the argument comp, if present; otherwise it is Pred().

the allocator object is the argument al, if present; otherwise, it is Alloc().

Example

 

// std_tr1__unordered_map__unordered_map_construct.cpp 
// compile with: /EHsc 
#include <unordered_map> 
#include <iostream> 

typedef std::unordered_map<char, int> Mymap; 
int main() 
    { 
    Mymap c1; 
 
    c1.insert(Mymap::value_type('a', 1)); 
    c1.insert(Mymap::value_type('b', 2)); 
    c1.insert(Mymap::value_type('c', 3)); 
 
// display contents " [c 3] [b 2] [a 1]" 
    for (Mymap::const_iterator it = c1.begin(); 
        it != c1.end(); ++it) 
        std::cout << " [" << it->first << ", " << it->second << "]"; 
    std::cout << std::endl; 
 
    Mymap c2(8, 
        std::tr1::hash<char>(), 
        std::equal_to<char>(), 
        std::allocator<std::pair<const char, int> >()); 
 
    c2.insert(Mymap::value_type('d', 4)); 
    c2.insert(Mymap::value_type('e', 5)); 
    c2.insert(Mymap::value_type('f', 6)); 
 
// display contents " [f 6] [e 5] [d 4]" 
    for (Mymap::const_iterator it = c2.begin(); 
        it != c2.end(); ++it) 
        std::cout << " [" << it->first << ", " << it->second << "]"; 
    std::cout << std::endl; 
 
    Mymap c3(c1.begin(), 
        c1.end(), 
        8, 
        std::tr1::hash<char>(), 
        std::equal_to<char>(), 
        std::allocator<std::pair<const char, int> >()); 
 
// display contents " [c 3] [b 2] [a 1]" 
    for (Mymap::const_iterator it = c3.begin(); 
        it != c3.end(); ++it) 
        std::cout << " [" << it->first << ", " << it->second << "]"; 
    std::cout << std::endl; 

    Mymap c4(std::move(c3));

// display contents " [c 3] [b 2] [a 1]" 
    for (Mymap::const_iterator it = c4.begin(); 
        it != c4.end(); ++it) 
        std::cout << " [" << it->first << ", " << it->second << "]"; 
    std::cout << std::endl; 
 
    return (0); 
    } 
 
 [c, 3] [b, 2] [a, 1]
 [f, 6] [e, 5] [d, 4]
 [c, 3] [b, 2] [a, 1]
 [c, 3] [b, 2] [a, 1]

Requirements

Header: <unordered_map>

Namespace: std

See Also

Reference

<unordered_map>

unordered_map Class

Other Resources

<unordered_map> Members