0

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?

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.