What is the equivalent of boost::make_transform_iterator in the standard library?(标准库中 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 的等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:标准库中 boost::make_transform_iterator 的等价物是什么?
基础教程推荐
- 我有静态或动态 boost 库吗? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 在 C++ 中计算滚动/移动平均值 2021-01-01
