std::transform
の問題は、一時オブジェクトの先頭と末尾の両方を取得できないことです。C++のPython風のマップ
私はPythonのようなマッピング関数をC++で実装したいと思います。これは型のベクトルで動作し、別の型(おそらく別の型)のベクトルにマップします。
これは私のアプローチです:
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;
}
そして、これは(フィルタの戻り値の型は、その最初の引数の型です)私はこれを使用する方法の例です:
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);
});
}
私もこのコードの取得エラーメッセージは次のとおりです。
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'
あなたは私にいくつかの助けを与えることができ、私はそれを修正しますか?また、コンパイル時の静的アサーションを使用して、操作の種類(T t)がUかどうかを確認できますか?
U
の取り外しとstd::vector<typename std::result_of<UnaryFunction(T)>::type> result;
と結果の宣言を置き換えるには、まだエラーが発生します。
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;
このコードはC++ 11では無効です。 –
@AdamHunyadi Verbose C++ 11バージョンが追加されました。 – Yakk
@AdamHunyadi Typosと残りのC++ 14のものが削除され、ライブのサンプルが含まれています。 – Yakk