What is the equivalent of boost::make_transform_iterator in the standard library?

你离开我真会死。 提交于 2020-05-26 10:34:46

问题


When dealing with a const vector, the following doesn't work:

const std::vector<std::string> v;
v.push_back("test"); // error: v cannot be modified

Instead, you have to initialize the vector on the same line where it is constructed. However, even with this restriction, boost::make_transform_iterator makes it easy to do something with another vector's elements before pushing them into v. In this example, convert is a unary function that returns a transformed version of an input element:

auto beg = boost::make_transform_iterator(args.begin(), convert);
auto end = boost::make_transform_iterator(args.end(), convert);

const std::vector<const char*> vc { beg, end };

I've taken a look at the functions available in <iterator> and don't see an equivalent. Is it simply missing or is there a reason why the standard library doesn't have it?


回答1:


For C++11 there's always the lambda inplace initialization trick:

const auto vc = [&]{
    std::vector<const char*> tmp(v.size());
    std::transform(v.begin(), v.end(), tmp.begin(), convert);
    return tmp;
}();

or

const auto vc = [&]{
    std::vector<const char*> tmp;
    tmp.reserve(v.size());
    std::transform(v.begin(), v.end(), back_inserter(tmp), convert);
    return tmp;
}();

See it Live On Coliru

That's said, I'd prefer the Boost Range adaptors: (also Live On Coliru)

const auto vc = boost::copy_range<std::vector<const char*> >(v | transformed(convert));
#include <algorithm>
#include <vector>
#include <iterator>
#include <string>

#include <functional>
#include <iostream>

int main()
{
    const std::vector</* const */ std::string> v { "test", "goodbye" };

    auto convert = std::mem_fn(&std::string::c_str);

    const auto vc = [&]{
        std::vector<const char*> tmp;
        tmp.reserve(v.size());
        std::transform(v.begin(), v.end(), back_inserter(tmp), convert);
        return tmp;
    }();

    for (auto cc : vc)
        std::cout << cc << "\n";
}


来源:https://stackoverflow.com/questions/26073388/what-is-the-equivalent-of-boostmake-transform-iterator-in-the-standard-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!