C++ Set Library - equal_range Function


Advertisements

Description

It returns the bounds of a range that includes all the elements in the container that are equivalent to val.

Declaration

Following are the ways in which std::set::equal_range works in various C++ versions.

C++98

iterator upper_bound (const value_type& val) const;

C++11

  iterator upper_bound (const value_type& val);
const_iterator upper_bound (const value_type& val) const;

Return value

It returns the bounds of a range that includes all the elements in the container that are equivalent to val.

Exceptions

If an exception is thrown, there are no changes in the container.

Time complexity

Time complexity depens on logarithmic.

Example

The following example shows the usage of std::set::equal_range.

#include <iostream>
#include <set>

int main () {
   std::set<int> myset;

   for (int i = 1; i <= 5; i++) myset.insert(i*10);

   std::pair<std::set<int>::const_iterator,std::set<int>::const_iterator> ret;
   ret = myset.equal_range(10);

   std::cout << "the lower bound points to: " << *ret.first << '\n';
   std::cout << "the upper bound points to: " << *ret.second << '\n';

   return 0;
}

The above program will compile and execute properly.

the lower bound points to: 10
the upper bound points to: 20
set.htm
Advertisements