
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Set equal_range() function in C++ STL
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
#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
- Related Articles
- Set find() function in C++ STL
- set lower_bound() function in C++ STL
- Set emplace_hint() function in C++ STL
- Set count() function in C++ STL
- Set max_size() function in C++ STL
- Set upper_bound() function in C++ STL
- set value_comp() function in C++ STL
- Set find() function in C++ programming STL
- Set cbegin() and cend() function in C++ STL
- Set crbegin() and crend() function in C++ STL
- Set get_allocator() in C++ STL
- Set insert() in C++ STL
- set operator= in C++ STL
- set::begin() and set::end() in C++ STL
- sinh() function in C++ STL
