# Set equal_range() function in C++ STL

C++Server Side ProgrammingProgramming

In this article we are going to discuss the set::equal_range() function in C++ STL, their syntax, working and their return values.

## What is Set in C++ STL?

Sets in C++ STL are the containers which must have unique elements in a general order. Sets must have unique elements because the value of the element identifies the element. Once added a value in a set container later can’t be modified, although we can still remove or add the values to the set. Sets are used as binary search trees.

## What is set::equal_range()

equal_range() function is an inbuilt function in C++ STL, which is defined in header file. This function returns the range of the set container which contains value passed as the argument of the function. The set contain all the unique values so the equivalent value in the found range will be single. If the value is not present in the container the range will be zero, with both iterators pointing to the first position.

## Syntax

Set1.equal_range(const type_t& value);

## Parameter

This function accepts one parameter, i.e., element which is to be found.

## Return value

This function returns the pair or we can say iterator range starting from the lower bound of the container till the element which is to be found in the container.

## Example

Input: set<int> myset = {10, 20, 30, 40};
Output: lower bound of 30 is 30

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
set<int> mySet;
mySet.insert(10);
mySet.insert(20);
mySet.insert(30);
mySet.insert(40);
mySet.insert(50);
cout<<"Elements before applying range() Function : ";
for (auto i = mySet.begin(); i != mySet.end(); i++)
cout << *i << " ";
auto i = mySet.equal_range(30);
cout<<"\nlower bound of 30 is "<< *i.first;
cout<<"\nupper bound of 30 is "<< *i.second;
i = mySet.equal_range(40);
cout<<"\nlower bound of 40 is " << *i.first;
cout<<"\nupper bound of 40 is " << *i.second;
i = mySet.equal_range(10);
cout<<"\nlower bound of 10 is " << *i.first;
cout<<"\nupper bound of 10 is " << *i.second;
return 0;
}

## Output

If we run the above code then it will generate the following output −

Elements before applying range() Function : 10 20 30 40 50
lower bound of 30 is 30
upper bound of 30 is 40
lower bound of 40 is 40
upper bound of 40 is 50
lower bound of 10 is 10
upper bound of 10 is 20
Published on 05-Mar-2020 14:32:51