为什么C++标准中没有 std::copy_until 功能

Why there is no std::copy_until function in C++ standard?

本文关键字:copy until 功能 std C++ 标准 为什么      更新时间:2023-10-16

C++标准库提供了非常棒的东西(std::copystd::countstd::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 算法不同,缺乏现有实现或扩展的经验,因此保守的选择是不添加它。

另请参阅"更多 STL 算法(修订版 2)"提案。尽管它没有提到类似copy_until的东西,但它揭示了一些为什么某些算法(如copy_if)被添加而其他算法(如lexicographical_compare_3way)没有。