unordered_map operator[] in C++ STL Last Updated : 14 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report The std::unordered_map::operator[] is a built in function in C++ STL which returns the reference of value if key matches in the container. If no key is found then it inserts that key into container. Syntax: mapped_type& operator[](key_type&& k); Parameter: It takes parameter as key whose mapped value is accessed. Return type: Returns a reference associated to that key. Example 1 CPP // C++ code to illustrate the method // unordered_map operator[] #include <bits/stdc++.h> using namespace std; int main() { unordered_map<int, int> sample; // Map initialization sample = { { 1, 2 }, { 3, 4 }, { 5, 6 } }; // print element before doing // any operations for (auto& it : sample) cout << it.first << " : " << it.second << endl; // existing element is read int m = sample[1]; // existing element is written sample[3] = m; // existing elements are accessed sample[5] = sample[1]; // non existing element // new element 25 will be inserted m = sample[25]; // new element 10 will be inserted sample[5] = sample[10]; // print element after doing // operations for (auto& it : sample) cout << it.first << " : " << it.second << endl; return 0; } Output: 5 : 6 3 : 4 1 : 2 10 : 0 1 : 2 5 : 0 3 : 2 25 : 0 Example 2 CPP // C++ code to illustrate the method // unordered_map operator[] #include <bits/stdc++.h> using namespace std; int main() { unordered_map<char, int> sample; // Map initialization sample = { { 'a', 2 }, { 'b', 4 }, { 'c', 6 } }; // print element before doing // any operations for (auto& it : sample) cout << it.first << " : " << it.second << endl; // existing element is read int m = sample['a']; // existing element is written sample['b'] = m; // existing elements are accessed sample['c'] = sample['a']; // non existing element // new element 'd' will be inserted m = sample['d']; // new element 'f' will be inserted sample['c'] = sample['f']; // print element after doing // operations for (auto& it : sample) cout << it.first << " : " << it.second << endl; return 0; } Output: c : 6 b : 4 a : 2 f : 0 a : 2 b : 2 c : 0 d : 0 Time Complexity O(n) in worst case. Comment More infoAdvertise with us Next Article Unordered Map in C++ STL A ankit15697 Follow Improve Article Tags : Technical Scripter C++ cpp-unordered_map cpp-unordered_map-functions Practice Tags : CPP Similar Reads unordered_map operator= in C++ STL The â=â is an operator in C++ STL which copies (or moves) an unordered_map to another unordered_map and unordered_map::operator= is the corresponding operator function. There are three versions of this function. The first version takes reference of an unordered_map as an argument and copies it to an 2 min read unordered_multimap operator= in C++ STL The â=â is an operator in C++ STL which copies (or moves) an unordered_multimap to another unordered_multimap and unordered_multimap::operator= is the corresponding operator function. There are three versions of this function. The first version takes reference of an unordered_multimap as an argument 2 min read unordered_set operator= in C++ STL The '=' is an operator in C++ STL which copies (or moves) an unordered_set to another unordered_set and unordered_set::operator= is the corresponding operator function. There are three versions of this function. The first version takes reference of an unordered_set as an argument and copies it to an 2 min read unordered_multimap operator= in C++ The unordered_multimap::operator= is a built-in function in C++ STL which does three types of tasks which are explained below. Syntax (copying elements from different container) : unordered_multimap_name1 operator= (unordered_multimap_name2)Parameters: The function does not accepts any parameter. Th 4 min read Unordered Map in C++ STL In C++, unordered_map is an unordered associative container that stores data in the form of unique key-value pairs. But unlike map, unordered map stores its elements using hashing. This provides average constant-time complexity O(1) for search, insert, and delete operations but the elements are not 7 min read unordered_map size() in C++ STL The unordered_multimap::size() is a built-in function in C++ Standard Template Library which return's the number of element in the unordered map. Syntax: unordered_multimap_name.size() Return Value: It returns the number of the element present in the unordered map. Time complexity: Constant i.e. O(1 1 min read Like