Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to compose STL algorithms without an intermediate container

Tags:

c++

My question is partially motivated by this question.

Is there a way to compose STL algorithms, or user made algorithms, without an intermediate container? An answer can use a tool from boost, but assume the composed algorithms are user made, or from the STL.

So boost::adaptors::reversed doesn't count since the reversing algorithm is in boost.

like image 820
Polymer Avatar asked Dec 16 '13 01:12

Polymer


1 Answers

No.

Let's say f and g are STL algorithms.

Let's say what you want is f(g(x)) (I'm trying to convey the idea here...).

There is no way to get around having a intermediate container, since the result of g(x) must be a container.

If you are going to avoid intermediate containers, then you must use algorithms that can "inspect" or interact with other algorithms, like Boost.Range adaptors (e.g. boost::adaptors::reversed).

For example, say f is "sort" and g is "reverse". Boost's adapters could figure out that the reverse step is a no-op and skip it. STL algorithms couldn't do that, since there's no way for that information to make it through.

like image 67
Paul Draper Avatar answered Sep 20 '22 11:09

Paul Draper