C++ 标准库提供了非常棒的东西(std::copy
、std::count
、std::remove
等),但我不知道为什么没有 std::copy_until
,它将数据从输入迭代器复制到输出,直到测试(例如 lambda)返回 true
。
这里是示例实现:
template<class inputIt, class outputIt, class comp_t>
outputIt copy_until(inputIt in, outputIt out, comp_t comp) {
while (comp(*in)) {
*out = *in;
++out;
++in;
}
return out;
}
我认为只是没有人写一篇论文或其他东西,但也许这样的功能在某种程度上很糟糕/表明我的代码设计错误?
最佳答案
“为什么 C++ 标准中没有 std::copy_until
函数?”的答案可能是以下之一:
- 没有人费心为此写一份提案。
- 有人提议,但 ISO 委员会认为它不够有用。
- 它被认为很有用,但与其他新的 C++11 算法不同,缺乏现有实现或扩展的经验,因此保守的选择是不添加它。
另请参阅"More STL algorithms (revision 2)" proposal 。尽管它没有提到像 copy_until
这样的东西,但它揭示了为什么添加某些算法(例如 copy_if
)和其他算法(例如 lexicographyal_compare_3way
) )不是。
关于c++ - 为什么C++标准中没有std::copy_until函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41618553/