Is it valid to use std::transform with std::back_inserter?

前端 未结 4 1354
北恋
北恋 2021-02-18 13:56

Cppreference has this example code for std::transform:

std::vector ordinals;
std::transform(s.begin(), s.end(), std::back_inserter(ordinals),
         


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-02-18 14:36

    I believe the transformation is guaranteed to be processed in-order. std::back_inserter_iterator is an output iterator (its iterator_category member type is an alias for std::output_iterator_tag) according to [back.insert.iterator].

    Consequently, std::transform has no other option on how to proceed to next iteration than to call member operator++ on the result parameter.

    Of course, this is valid only for overloads without execution policy, where std::back_inserter_iterator may not be used (it's not a forwarding iterator).


    BTW, I wouldn't argument with quotes from cppreference. The statements there are often imprecise or simplified. In cases as these, it's better to look at the C++ Standard. Where, regarding to std::transform, there is no quote about order of operations.

提交回复
热议问题