网上似乎有很多关于python 3.0中reduce()函数更改以及如何删除它的讨论.我有点难以理解为什么会这样; 我发现在各种情况下使用它是相当合理的.如果蔑视只是主观的,我无法想象会有这么多人关心它.
我错过了什么?reduce()有什么问题?
可能重复:
使用Boost.Phoenix有什么好处?
所以我开始阅读boost phoenix的文档.
但是,我必须承认我不太了解库的目的,特别是因为我们在C++ 0x中对lambdas有语言支持.
有人可以解释或给我一个例子吗?
如果我知道如何提取匹配类型,是否有一种现代方式表达从不同类型的源容器有条件地复制到目标容器的意图?
将问题作为代码示例提出更容易:
#include <algorithm>
#include <vector>
struct Foo {};
struct FooBar{
bool is_valid;
Foo foo;
};
std::vector<Foo> get_valid_foos(const std::vector<FooBar>& foobars){
std::vector<Foo> valid_foos;
for(const auto& fbar : foobars){
if(fbar.is_valid)
valid_foos.push_back(fbar.foo);
}
return valid_foos;
}
std::vector<Foo> get_valid_foos_modern(const std::vector<FooBar>& foobars){
std::vector<Foo> valid_foos;
std::copy_if(foobars.begin(), foobars.end(), std::back_inserter(valid_foos),
[](const auto& foobar){
return foobar.is_valid;
});
//?? std::copy requires input and output types to match
return valid_foos;
}
Run Code Online (Sandbox Code Playgroud)
完全公开,这可能是在没有需要时尝试使用STL算法的锤子和钉子情况.我在一些C++ 14代码中看到了一个重新出现的模式.我们有一个迭代的容器,如果当前元素匹配某些条件,那么我们将其中一个元素字段复制到另一个容器.
模式类似于:
for (auto it = std::begin(foo); it!=std::end(foo); ++it){
auto x = it->Some_member;
// Note, the check usually uses the field would add to the new container.
if(f(x) && g(x)){
bar.emplace_back(x);
}
}
Run Code Online (Sandbox Code Playgroud)
这个想法几乎是一个累积,其中所应用的函数并不总是返回一个值.我只能想到一个解决方案
这甚至是个好主意吗?
在boost::adaptors::filteredfilter函数中的用法如下:
std::vector<int> input;
input += 1,2,3,4,5,6,7,8,9;
boost::copy(
input | filtered(is_even()),
std::ostream_iterator<int>(std::cout, ","));
Run Code Online (Sandbox Code Playgroud)
在这种情况下,管道操作员会产生什么影响?它没有为定义std::vector,它是过载吗?如果是这样,一个人如何有效地寻找诸如boost之类的自由经营者?