Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Finds or inserts an element with the specified key.
Ty& operator[](const Key& keyval);
Parameters
- keyval
The key value to find or insert.
Example
// std_tr1__unordered_map__unordered_map_operator_sub.cpp
// compile with: /EHsc
#include <unordered_map>
#include <iostream>
typedef std::tr1::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;
// try to find and fail
std::cout << "c1['A'] == " << c1['A'] << std::endl;
// try to find and succeed
std::cout << "c1['a'] == " << c1['a'] << std::endl;
// redisplay contents
for (Mymap::const_iterator it = c1.begin();
it != c1.end(); ++it)
std::cout << " [" << it->first << ", " << it->second << "]";
std::cout << std::endl;
return (0);
}
[c, 3] [b, 2] [a, 1] c1['A'] == 0 c1['a'] == 1 [c, 3] [b, 2] [A, 0] [a, 1]
Remarks
The member function determines the iterator where as the return value of unordered_map::insert( unordered_map::value_type(keyval, Ty()). (It inserts an element with the specified key if no such element exists.) It then returns a reference to (*where).second.
Requirements
Header: <unordered_map>
Namespace: std::tr1