标准库中 boost::make_transform_iterator 的等价物是什么?

2022-01-10 00:00:00 iterator c++ c++11 boost

在处理 const 向量时,以下内容不起作用:

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

相反,您必须在构造向量的同一行初始化向量.然而,即使有这个限制,boost::make_transform_iterator 可以很容易地在将另一个向量的元素推入 v 之前对其进行处理.在这个例子中,convert 是一个一元函数返回输入元素的转换版本:

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

我查看了 <iterator> 中可用的函数,但没有看到等效函数.它只是缺少它还是标准库没有它的原因?

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?

推荐答案

对于 C++11,总是有 lambda 就地初始化技巧:

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;
}();

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

看到它Live On Coliru

See it Live On Coliru

也就是说,我更喜欢 Boost Range 适配器:(也 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 << "
";
}

相关文章