【发布时间】:2016-04-19 16:58:14
【问题描述】:
例如的非迭代器版本all_of 可以写成:
template <class Container, class UnaryPredicate>
bool all_of(Container s, UnaryPredicate f) {
return all_of(s.begin(), s.end(), f);
}
但我认为你不能对返回容器的算法做同样的事情?
template <class Container, class UnaryPredicate>
Container copy_if(Container, UnaryPredicate);
我最接近实现的是使用向量来保存中间结果,但由于缺乏为向量提供模板参数的任何方法而绊倒了。我有什么遗漏吗?
【问题讨论】:
-
为什么要使用
vector来保存临时结果,而不是直接使用Container? -
@Holt 因为当我尝试直接使用容器时,在 std::set 的情况下,它不起作用;编译器似乎认为 set 的迭代器是只读的,这确实有些道理。
标签: c++ stl-algorithm