unordered_map equal_range in C++ Last Updated : 27 Nov, 2018 Comments Improve Suggest changes Like Article Like Report The unordered_map::equal_range() is an inbuilt function in C++ STL which is used to return the bounds of a range that includes all the elements in the container with a key that compares equal to k. The unordered_map containers are the container where keys are unique, the range will include one element at most. The range is defined by two iterators, The first one pointing to the first element of the range. The second one pointing past the last element of the range. Parameters: This function accepts single parameter key which is used to hold the value to be compared. Return Value: It returns a pair which contains a pair of iterators defining the wanted range. Where its members are pair::first and pair::second. The first one is an iterator to the lower bound of the range and the second one is an iterator to its upper bound. The elements in the range are those between these two iterators, including first pair, but not second. Below programs illustrates the unordered_map::equal_range() function in C++ STL: Example 1: CPP // C++ program to implement // unordered_map::equal_range() function #include <iostream> #include <unordered_map> using namespace std; // main program int main() { unordered_map <int, int> map = { { 1, 3 }, { 1, 2 }, { 3, 1 }, { 2, 3 } }; for (int j = 1; j <= 3; j++) { auto range = map.equal_range(j); //'auto' is a keyword for (auto i = range.first; i != range.second; i++) { // iterates first to last cout << "first : " << i->first; cout << "\nsecond : " << i->second << endl << endl; } } } Output: first : 1 second : 3 first : 2 second : 3 first : 3 second : 1 Program 2: CPP // C++ program to search 'unordered map' container #include <iostream> #include <unordered_map> using namespace std; // Rename 'unordered_map<int, char>' as 'gfg' typedef unordered_map<char, char> gfg; int main() { // 'g' is object gfg g; // Container values // Contents are look like [a, b] [c, d] [e, f] g.insert(gfg::value_type('a', 'b')); g.insert(gfg::value_type('b', 'd')); g.insert(gfg::value_type('e', 'f')); // Look into the syntax part above // here 'f' is key pair<gfg::iterator, gfg::iterator> p1 = g.equal_range('f'); // 'f' is not exits, so no output for 'f' cout << "search for 'f' :"; for (; p1.first != p1.second; ++p1.first) { cout << p1.first->first << ", " << p1.first->second << endl; } // Successful search // Here 'a' is key p1 = g.equal_range('a'); // 'a' is exits, so it searched successfully cout << "\nsearch for 'a' : ["; for (; p1.first != p1.second; ++p1.first) { cout << p1.first->first << ", " << p1.first->second << "]"; } return 0; } Output: search for 'f' : search for 'a' : [a, b] Complexity: Average case: Linear in the number of elements with the key k, which is constant. worst case: Linear in the size of the container. Comment More infoAdvertise with us S SoumikMondal Follow Improve Article Tags : C++ Technical Scripter 2018 STL cpp-unordered_map cpp-unordered_map-functions +1 More Practice Tags : CPPSTL Explore C++ Programming Language 5 min read C++ OverviewIntroduction to C++ Programming Language 3 min read Features of C++ 5 min read History of C++ 7 min read Interesting Facts about C++ 2 min read Setting up C++ Development Environment 8 min read Difference between C and C++ 3 min read C++ BasicsUnderstanding First C++ Program 4 min read C++ Basic Syntax 4 min read C++ Comments 3 min read Tokens in C 4 min read C++ Keywords 2 min read Difference between Keyword and Identifier in C 3 min read C++ Variables and ConstantsC++ Variables 4 min read Constants in C 4 min read Scope of Variables in C++ 7 min read Storage Classes in C++ with Examples 6 min read Static Keyword in C++ 5 min read C++ Data Types and LiteralsC++ Data Types 7 min read Literals in C 4 min read Derived Data Types in C++ 4 min read User Defined Data Types in C++ 4 min read Data Type Ranges and Their Macros in C++ 3 min read C++ Type Modifiers 4 min read Type Conversion in C++ 4 min read Casting Operators in C++ 5 min read C++ OperatorsOperators in C++ 9 min read C++ Arithmetic Operators 4 min read Unary Operators in C 5 min read Bitwise Operators in C 6 min read Assignment Operators in C 4 min read C++ sizeof Operator 3 min read Scope Resolution Operator in C++ 4 min read C++ Input/OutputBasic Input / Output in C++ 5 min read cin in C++ 4 min read cout in C++ 2 min read Standard Error Stream Object - cerr in C++ 2 min read Manipulators in C++ 4 min read C++ Control StatementsDecision Making in C (if , if..else, Nested if, if-else-if ) 7 min read C++ if Statement 3 min read C++ if else Statement 3 min read C++ if else if Ladder 3 min read Switch Statement in C++ 5 min read Jump statements in C++ 4 min read C++ Loops 7 min read for Loop in C++ 6 min read Range-Based for Loop in C++ 3 min read C++ While Loop 3 min read C++ do while Loop 4 min read C++ FunctionsFunctions in C++ 8 min read return Statement in C++ 4 min read Parameter Passing Techniques in C 3 min read Difference Between Call by Value and Call by Reference in C 4 min read Default Arguments in C++ 5 min read Inline Functions in C++ 6 min read Lambda Expression in C++ 4 min read C++ Pointers and ReferencesPointers and References in C++ 5 min read C++ Pointers 8 min read Dangling, Void , Null and Wild Pointers in C 6 min read Applications of Pointers in C 4 min read Understanding nullptr in C++ 3 min read References in C++ 5 min read Can References Refer to Invalid Location in C++? 2 min read Pointers vs References in C++ 5 min read Passing By Pointer vs Passing By Reference in C++ 5 min read When do we pass arguments by pointer? 5 min read Like