Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is std::set::equal_range for?

Tags:

c++

stl

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.

like image 258
rubix_addict Avatar asked Feb 18 '18 20:02

rubix_addict


People also ask

What does Equal_range return in C++?

equal_range in C++ std::equal_range is used to find the sub-range within a given range [first, last) that has all the elements equivalent to a given value. It returns the initial and the final bound of such a sub-range.

What is std :: set?

std::set is an associative container that contains a sorted set of unique objects of type Key . Sorting is done using the key comparison function Compare. Search, removal, and insertion operations have logarithmic complexity. Sets are usually implemented as red-black trees.

What is the return type of the STL Equal_range function?

The set::equal_range() is a built-in function in C++ STL which returns an iterator of pairs.

Is std :: set ordered C++?

so yes, order is guaranteed by the C++ standard.


1 Answers

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.

like image 185
Ferruccio Avatar answered Oct 08 '22 21:10

Ferruccio