STL algorithms: Why no additional interface for containers (additional to iterator pairs)?

They do introduce ambiguity for many algorithms. A lot of <algorithm> looks like template<class iterator> void do_something(iterator, iterator); template<class iterator, class funct> void do_something(iterator, iterator, funct); If you add additional overloads template<class container, class funct> void do_something(container, funct); the compiler will have some trouble figuring out what do_something(x, y) means. If x and y are … Read more

How to find the intersection of two STL sets?

You haven’t provided an output iterator for set_intersection template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result ); Fix this by doing something like …; set<int> intersect; set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), std::inserter(intersect, intersect.begin())); You need a std::insert iterator since the set is as of … Read more

Why is there no transform_if in the C++ standard library?

The standard library favours elementary algorithms. Containers and algorithms should be independent of each other if possible. Likewise, algorithms that can be composed of existing algorithms are only rarely included, as shorthand. If you require a transform if, you can trivially write it. If you want it /today/, composing of ready-mades and not incur overhead, … Read more

How to remove duplicates from unsorted std::vector while keeping the original ordering using algorithms?

Sounds like a job for std::copy_if. Define a predicate that keeps track of elements that already have been processed and return false if they have. If you don’t have C++11 support, you can use the clumsily named std::remove_copy_if and invert the logic. This is an untested example: template <typename T> struct NotDuplicate { bool operator()(const … Read more

Using local classes with STL algorithms

It’s explicitly forbidden by the C++98/03 standard. C++11 remove that restriction. To be more complete : The restrictions on types that are used as template parameters are listed in article 14.3.1 of the C++03 (and C++98) standard: A local type, a type with no linkage, an unnamed type or a type compounded from any of … Read more