equal_range [C++ Reference]
Keywords: stl,set,equal_range
Date: 2009-09-05T07:54:11-0700

[[equal_range]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- fr
- it
- jp
- pl
- ru
- tr

## equal_range

Syntax:

        #include <set>
        pair<iterator, iterator> equal_range( const key_type& key ) const;

The function equal_range() returns two iterators - one to the first element that contains key, another to a point just after the last element that contains key. In set containers, where no duplicate keys are allowed, the range will include one element at most.

If key does not match any key in the container, both the returned pointers point to nearest value greater than key, if any, or to set::end if key is greater than all the elements in the container.

Example:

    // set::equal_elements
    #include <iostream>
    #include <set>
    using namespace std;

    int main ()
    {
      set<int> myset;
      pair<set<int>::iterator,set<int>::iterator> ret;

      for (int i=1; i<=5; i++) myset.insert(i*10);   // set: 10 20 30 40 50

      ret = myset.equal_range(30);

      cout << "lower bound points to: " << *ret.first << endl;
      cout << "upper bound points to: " << *ret.second << endl;

      return 0;
    }

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •