Question

Several STL algorithms have the general form:

Algorithm(InputIterator first1, InputIterator last1, OutputIterator result,...)

or

Algorithm(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result,...)

My question is - when can the OutputIterator result be in the range (first1,last1) or (first2,last2)?

For transform the answer I found here: http://www.cplusplus.com/reference/algorithm/transform/

But there are multiple others that may make sense, e.g. set_difference should theoretically be safe for such a usage and also it works in code with GCC 4.7.1.

Any references?

Was it helpful?

Solution

There is no general answer for all algorithms. For set_difference you can learn from C++11 25.4.5.4/2 that:

Requires: The resulting range shall not overlap with either of the original ranges. This seems to make your code undefined behavior that seems to do what you want.

You'll just have to look at the requirements for a particular algorithm before you decide what iterator ranges to use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top