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

What is the equivalent of boost::make_transform_iterator in the standard library?(标准库中 boost::make_transform_iterator 的等价物是什么?)
本文介绍了标准库中 boost::make_transform_iterator 的等价物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理 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 << "
";
}

这篇关于标准库中 boost::make_transform_iterator 的等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)