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)?
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.
Return valueOutput iterator to the element past the last element transformed.
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);
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 toop(*(first1 + (i - result))
orbinary_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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With