Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 << "
";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...