Re: Should (and can) the <algorithm> functions be extended by convenience helpers that take generic containers as parameters?
Martin B. wrote:
Hi!
Note: I am pretty sure this has been brought up in various places a
lot of times, but it's a tad hard to google for. :-)
Many [algorithm
functions](http://www.cplusplus.com/reference/algorithm/) work with
a pair (or pairs) of iterators.
Often, these iterators will be used with container.begin() and
container.end().
Wouldn't it therefore make a lot of sense to have additional helper
functions that just take a generic container argument instead of a
pair of iterators?
Example:
//from <algorithm>:
template <class RandomAccessIterator, class Compare>
void sort ( RandomAccessIterator first,
RandomAccessIterator last,
Compare comp );
//useful(??) additional version:
template <class RandomAccessContainer, class Compare>
void sort ( RandomAccessContainer cont,
Compare comp )
{
sort(cont.begin(), cont.end(), comp);
}
Personally, I think this should just be part of the standard
library.
Barring this, does any such convenience header already exists, that
tries to add such helpers where they make sense?
There were proposals for this when the C++0x draft still contained
"concepts". In the general case, you would need the "concepts" to
decide what function to call. Otherwise there might be problems for
the compiler to decide among function like:
template <class RandomAccessContainer, class Compare>
void sort ( RandomAccessContainer cont, Compare comp );
template <class RandomAccessIterator>
void sort ( RandomAccessIterator first, RandomAccessIterator last);
How do we know what is an iterator, a container, or a comparer?
Bo Persson
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]