Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::transform order guarantees

Tags:

c++

c++11

On this page there is this note:

std::transform does not guarantee in-order application of unary_op or binary_op.

Does this mean that the resulting order of the sequence is not guaranteed to correlate to the order of the input sequence, or, does it mean that, while the order of the final result of the transform is guaranteed, the individual elements could have been created out of order (though they will still appear in-order)?

like image 815
johnbakers Avatar asked Dec 08 '15 22:12

johnbakers


People also ask

Is transform faster than for loop?

List comprehension with a separate transform() function is around 17% slower than the initial "for loop"-based version (224/191≈1.173). But it's much more readable, so I prefer it over the other solutions.

What does std transform return?

Return valueOutput iterator to the element past the last element transformed.

How do you use std transform?

std::transform on a range For example, to obtain the keys that a map contains, you can use std::transform the following way: map<int, string> m = { {1,"foo"}, {42, "bar"}, {7, "baz"} }; vector<int> keys; std::transform(m. begin(), m. end(), std::back_inserter(keys), getFirst);


1 Answers

The order of the resulting sequence is fixed. Specifically the standard says:

Effects: Assigns through every iterator i in the range [result,result + (last1 - first1)) a new corresponding value equal to op(*(first1 + (i - result)) or binary_op(*(first1 + (i - result)), *(first2 + (i - result))).

This guarantees that the first element of the result range will be obtained by transforming the first element(s) of the input range(s) and so on. However, the order in which the calls to op are made is not specified.

like image 105
Brian Bi Avatar answered Oct 01 '22 03:10

Brian Bi