표준 C++ lib에서 map
기능을 찾지 못해서 놀랐습니다. 아래라고으로,이 기능을 필터링하는 것 : 지금은이 솔루션매핑 기능의 C++ 아날로그
template <typename Container, typename InputIterator, typename UnaryPredicate>
Container filter(InputIterator _from, InputIterator _to, UnaryPredicate _pred)
{
Container collection;
return std::accumulate(_from, _to, collection,
[_pred] (Container acc, const InputIterator::value_type & val) -> Container
{
if (_pred(val))
acc.insert(std::end(acc), val);
return acc;
});
}
//////////////////////////////
// usage
std::vector<int> vec = {0, 1, 2, 3};
std::vector<int> newVec = filter<decltype(newVec)>(std::begin(vec), std::end(vec),
[] (int n)
{
return n % 2 == 0;
});
하지만 어쩌면 좀 더 일반적인 솔루션
편집 존재를 사용하고 있습니다. 좋아, 내 map
구현 : 그들은 소스 컬렉션을 변경 std::transform
와
template <typename T, typename MapFunction>
T map(T source, MapFunction func)
{
T collection;
for (auto val : source)
{
collection.insert(std::end(collection), func(val));
}
return collection;
}
그래서 문제 등,하지만 그들은 다른 하나를 반환해야합니다.
왜 '필터'에 대한 구현을 보여 주면서'map' 함수에 대해 묻고 있습니까? – juanchopanza
C++에는 각 요소의 함수를 호출하기위한'std :: for_each'가 있습니다. – chris
@juanchopanza 필터는 단지 술어가있는지도 일뿐입니다. 오해 미안 해요, 나 둘 다 거의 동일합니다. –