Share via


unordered_map::at

Finds an element in a unordered_map with a specified key value.

Ty& at(const Key& _Key);
const Ty& at(const Key& _Key) const;

Parameters

Parameter

Description

_Key

The key value to find.

Return Value

A reference to the data value of the element found.

Remarks

If the argument key value is not found, then the function throws an object of class out_of_range.

Example

// unordered_map_at.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)); 
 
// find and show elements
    std::cout << "c1.at('a') == " << c1.at('a') << std::endl; 
    std::cout << "c1.at('b') == " << c1.at('b') << std::endl; 
    std::cout << "c1.at('c') == " << c1.at('c') << std::endl; 

    return (0); 
    } 

Output

c1.at('a') == 10
c1.at('b') == 20
c1.at('c') == 30

Requirements

Header: <unordered_map>

Namespace: std

See Also

Reference

<unordered_map>

hash_map Class

Standard Template Library

Other Resources

<unordered_map> Members

hash_map Members