unordered_map operator[] in C++ STL Last Updated : 14 Dec, 2018 Comments Improve Suggest changes 1 Likes 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. Create Quiz Comment A ankit15697 Follow 1 Improve A ankit15697 Follow 1 Improve Article Tags : Technical Scripter C++ cpp-unordered_map cpp-unordered_map-functions Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like