[英]Python-like map in C++
My problem with std::transform
is that I can't both get the beginning and the end of a temporary object. 我的
std::transform
问题是我无法同时获得临时对象的开始和结束。
I would like to implement a Python-like mapping function in C++ that works on vectors of a type and maps them to another vector (of possibly another type). 我想在C ++中实现一个类似Python的映射函数,该函数可用于一种类型的向量并将它们映射到另一个向量(可能是另一种类型)。
This is my approach: 这是我的方法:
template <class T, class U, class UnaryOperator>
std::vector<T> map(const std::vector<T>& vectorToMap, UnaryOperator operation)
{
std::vector<U> result;
result.reserve(vectorToMap.size());
std::transform(vectorToMap.begin(), vectorToMap.end(),
std::back_inserter(result), [&operation] (U item) { return operation(item); });
return result;
}
And this is an example of how I intend to use this (where the return type of filter is the type of its first argument): 这是我打算如何使用此示例(其中filter的返回类型是其第一个参数的类型):
std::vector<std::shared_ptr<Cluster>> getClustersWithLength(const std::vector<Cluster>& clusterCollection, const int& length)
{
return map(filter(clusterCollection, [&length] (Cluster& cluster) {
return cluster.sizeY == length;
}),
[] (const Cluster& cluster) {
return std::make_shared<Cluster>(cluster);
});
}
The error message I get for this code though is: 我收到此代码的错误消息是:
error: no matching function for call to 'map(std::vector<Cluster>,
ClusterPairFunctions::getClustersWithLength(const
std::vector<Cluster>&, const int&)::<lambda(const Cluster&)>)'
note: candidate: template<class T, class U, class UnaryOperator> std::vector<_RealType> map(const std::vector<_RealType>&, UnaryOperator)
std::vector<T> map(const std::vector<T>& vectorToMap, UnaryOperator operation)
note: couldn't deduce template parameter 'U'
Can you give me some help, how do I fix it? 您能给我些帮助吗,我该如何解决? Also, can I somehow use compile-time static assertion to check if the type of operation(T t) is U?
另外,我可以以某种方式使用编译时静态断言来检查操作类型(T t)是否为U吗?
Removing U
and replacing the declaration of result with std::vector<typename std::result_of<UnaryFunction(T)>::type> result;
删除
U
并用std::vector<typename std::result_of<UnaryFunction(T)>::type> result;
替换结果声明std::vector<typename std::result_of<UnaryFunction(T)>::type> result;
still produces an error: 仍然产生错误:
src/ClusterPairFunctions.cc: In function 'std::vector<std::shared_ptr<Cluster> > ClusterPairFunctions::getClustersWithLength(const std::vector<Cluster>&, const int&)':
src/ClusterPairFunctions.cc:130:14: error: could not convert 'map(const std::vector<_RealType>&, UnaryFunction) [with T = Cluster; UnaryFunction = ClusterPairFunctions::getClustersWithLength(const std::vector<Cluster>&, const int&)::<lambda(const Cluster&)>]((<lambda closure object>ClusterPairFunctions::getClustersWithLength(const std::vector<Cluster>&, const int&)::<lambda(const Cluster&)>{}, ClusterPairFunctions::getClustersWithLength(const std::vector<Cluster>&, const int&)::<lambda(const Cluster&)>()))' from 'std::vector<Cluster>' to 'std::vector<std::shared_ptr<Cluster> >'
return (map(filter(clusterCollection, [&length] (Cluster& cluster) {
In file included from src/../interface/ClusterPairFunctions.h:5:0,
from src/ClusterPairFunctions.cc:1:
src/../interface/../../../interface/HelperFunctionsCommon.h: In instantiation of 'std::vector<_RealType> filter(const std::vector<_RealType>&, UnaryPredicate) [with T = Cluster; UnaryPredicate = ClusterPairFunctions::getClustersWithLength(const std::vector<Cluster>&, const int&)::<lambda(Cluster&)>]':
src/ClusterPairFunctions.cc:132:4: required from here
src/../interface/../../../interface/HelperFunctionsCommon.h:52:15: error: no match for call to '(ClusterPairFunctions::getClustersWithLength(const std::vector<Cluster>&, const int&)::<lambda(Cluster&)>) (const Cluster&)'
if(predicate(*it)) result.push_back(*it);
^
src/ClusterPairFunctions.cc:130:68: note: candidate: ClusterPairFunctions::getClustersWithLength(const std::vector<Cluster>&, const int&)::<lambda(Cluster&)> <near match>
return (map(filter(clusterCollection, [&length] (Cluster& cluster) {
^
src/ClusterPairFunctions.cc:130:68: note: conversion of argument 1 would be ill-formed:
In file included from src/../interface/ClusterPairFunctions.h:5:0,
from src/ClusterPairFunctions.cc:1:
src/../interface/../../../interface/HelperFunctionsCommon.h:52:15: error: binding 'const Cluster' to reference of type 'Cluster&' discards qualifiers
if(predicate(*it)) result.push_back(*it);
^
src/../interface/../../../interface/HelperFunctionsCommon.h: In instantiation of 'std::vector<_RealType> map(const std::vector<_RealType>&, UnaryFunction) [with T = Cluster; UnaryFunction = ClusterPairFunctions::getClustersWithLength(const std::vector<Cluster>&, const int&)::<lambda(const Cluster&)>]':
src/ClusterPairFunctions.cc:135:4: required from here
src/../interface/../../../interface/HelperFunctionsCommon.h:64:9: error: could not convert 'result' from 'std::vector<std::shared_ptr<Cluster> >' to 'std::vector<Cluster>'
return result;
Here is your code made a touch more generic: 这是使您的代码更加通用的代码:
template <template<class...>class Z=std::vector, class C, class UnaryOperator>
auto fmap(C&& c_in, UnaryOperator&& operation)
{
using dC = std::decay_t<C>;
using T_in = dC::reference;
using T_out = std::decay_t< std::result_of_t< UnaryOperator&(T_in) > >;
using R = Z<T_out>;
R result;
result.reserve(vectorToMap.size());
using std::begin; using std::end;
std::transform(
begin(cin), end(cin),
std::back_inserter(result),
[&] (auto&& item) { return operation(declype(item)(item)); }
);
return result;
}
To make the above work in C++11, you'll have to add trailing return type -> decltype(complex expression)
and replace the nice std::decay_t<whatever>
with typename std::decay<whatever>::type
or write your own aliases. 要使以上代码在C ++ 11中工作,您必须添加尾随返回类型
-> decltype(complex expression)
,并将好用的std::decay_t<whatever>
替换为typename std::decay<whatever>::type
或编写自己的别名。
These steps: 这些步骤:
using dC = std::decay<C>;
using T_in = dC::reference;
using T_out = std::decay_t< std::result_of_t< UnaryOperator&(T_in) > >;
using R = Z<T_out>;
need to be moved to a helper type 需要移至辅助类型
template<template<class...>class Z, class C, class Op>
struct calculate_return_type {
using dC = typename std::decay<C>::type;
using T_in = typename dC::reference;
using T_out = typename std::decay< typename std::result_of< Op&(T_in) >::type >::type;
using R = Z<T_out>;
};
giving us this: 给我们这个:
template <template<class...>class Z=std::vector, class C, class UnaryOperator>
auto fmap(C&& c_in, UnaryOperator&& operation)
-> typename calculate_return_type<Z, C, UnaryOperator>::R
{
using R = typename calculate_return_type<Z, C, UnaryOperator>::R;
R result;
result.reserve(c_in.size());
using T_in = typename calculate_return_type<Z, C, UnaryOperator>::T_in;
using std::begin; using std::end;
std::transform(
begin(c_in), end(c_in),
std::back_inserter(result),
[&] (T_in item) { return operation(decltype(item)(item)); }
);
return result;
}
but really, it is 2016, do attempt to upgrade to C++14. 但实际上是2016年,请尝试升级到C ++ 14。
In C++14, I find curry-style works well 在C ++ 14中,我发现咖喱风格的效果很好
template<class Z, class T>
struct rebind_helper;
template<template<class...>class Z, class T_in, class...Ts, class T_out>
struct rebind_helper<Z<T_in,Ts...>, T_out> {
using type=Z<T_out, Ts...>;
};
template<class Z, class T>
using rebind=typename rebind_helper<Z,T>::type;
template<class Op>
auto fmap( Op&& op ) {
return [op = std::forward<Op>(op)](auto&& c) {
using dC = std::decay_t<decltype(c)>;
using T_in = dC::reference;
using T_out = std::decay_t< std::result_of_t< UnaryOperator&(T_in) > >;
using R=rebind< dC, T_out >;
R result;
result.reserve(vectorToMap.size());
using std::begin; using std::end;
std::transform(
begin(cin), end(cin),
std::back_inserter(result),
[&] (auto&& item) { return operation(declype(item)(item)); }
);
return result;
};
}
both of these need a "reserve if possible" function (which does SFINAE to detect if .reserve
exists, and if so reserves; otherwise, doesn't bother). 两者都需要一个“如果可能,则保留”功能(由SFINAE进行检测,以了解是否存在
.reserve
,如果存在则保留;否则,就不会打扰)。
The second looks like: 第二个看起来像:
auto fmap_to_double = fmap( [](auto in){ return (double)in; } );
which can then be passed a container and it remaps its elements to double
. 然后可以将其传递给容器,并将其元素重新映射为
double
。
auto double_vector = fmap_to_double( int_vector );
On the other hand, maybe always producing vectors might be a worthwhile simplification. 另一方面,也许总是产生载体可能是一个值得简化的过程。 However, always only consuming vectors seems pointless.
但是,总是仅消耗向量似乎毫无意义。
不用将U
作为模板参数,您可以像这样简单地声明结果向量:
std::vector<typename std::result_of<UnaryFunction(T)>::type>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.