Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values.
This operator is used to reference the element present at position given inside the operator. It is similar to the at() function, the only difference is that the at() function throws an out-of-range exception when the position is not in the bounds of the size of map, while this operator causes undefined behaviour.
mapname[key] Parameters : Key value mapped to the element to be fetched. Returns : Direct reference to the element at the given key value.
Input : map mymap; mymap['a'] = 1; mymap['a']; Output : 1 Input : map mymap; mymap["abcd"] = 7; mymap["abcd"]; Output : 7
Errors and Exceptions
1. If the key is not present in the map, it shows undefined behaviour.
2. It has a no exception throw guarantee otherwise.
Time Complexity: O(logn)
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Split an array containing N elements into K sets of distinct elements
- Check if given permutation of 1 to N is feasible using given operations
- Find the K closest points to origin using Priority Queue
- Sorting a Map by value in C++ STL
- How to iterate through a Vector without using Iterators in C++
- Code Bloating in C++ with Examples
- Implementation of lower_bound and upper_bound on Set of Pairs in C++
- Implementation of lower_bound() and upper_bound() on Map of Pairs in C++
- std::cerr | Error Stream Object in C++ with Examples
- Methods vs. Functions in C++ with Examples
- basic_istream::readsome() in C++ with Examples
- std::is_function template in C++ with Examples
- type_traits::is_null_pointer in C++
- std::extent() template in C++ with Examples
- basic_istream::swap() in C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.