11

Since std::set cannot contain duplicate elements and is always sorted, std::set::equal_range will always return the range that has either none or 1 element. Technically, yes, that's still a range, but what is the purpose of this algorithm? For std::set it seems pretty unnecessary.

rubix_addict
  • 1,811
  • 13
  • 27

3 Answers3

9

I'm only guessing. But, like count(), it has some value when you're in a template and don't want to have to determine whether you're operating on a std::set or some other associative container.

Basically, it's for consistency. The function does perform as advertised, it's just that yes it has questionable use versus something like find() if you take it in isolation. It does potentially save you one manual iterator increment if you really do want a half-open range out of the box though. :P

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
9

All of the associative containers support equal_range, which means you can write generic code which accepts a set, multiset, map or multimap and it will do the right thing.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
0

Consider std::multiset. Despite practically identical in interface, it can contain equal_ranges of higher lengths.

bipll
  • 11,747
  • 1
  • 18
  • 32