为什么没有 std::copy_if 算法?

发布于 2024-08-05 09:14:22 字数 143 浏览 1 评论 0原文

C++ 中没有 std::copy_if 算法有什么具体原因吗?我知道我可以使用 std::remove_copy_if 来实现所需的行为。我认为它会出现在 C++0x 中,但是一个带有范围、输出迭代器和函子的简单的 copy_if 就很好了。是只是错过了还是有其他原因?

Is there any specific reason for not having std::copy_if algorithm in C++ ? I know I can use std::remove_copy_if to achieve the required behavior. I think it is coming in C++0x, but a simple copy_if which takes a range, a output iterator and a functor would have been nice. Was it just simply missed out or is there some other reason behind it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

天生の放荡 2024-08-12 09:14:23

多个 来源 表明它被意外地排除在STL之外。

然而,我不确定这是事实还是一个自我延续的神话。如果有人能指出比互联网上随机帖子的链接更可靠的来源,我将不胜感激。

Multiple sources indicate that it was left out of STL by accident.

However, I am not sure if that's a fact or a self-perpetuating myth. I'd appreciate if anyone would point out a source more credible than a link to a random post on the Internet.

一桥轻雨一伞开 2024-08-12 09:14:23

编写自己的谓词非常容易:

template <class InputIterator, class OutputIterator, class Predicate>
OutputIterator copy_if(InputIterator first, InputIterator last,
                       OutputIterator result, Predicate pred)
{
  return std::remove_copy_if(first,last,result,std::not1(pred));
}

编辑: 此版本适用于所有谓词:

template <class InputIterator, class OutputIterator, class Predicate>
OutputIterator copy_if(InputIterator first, InputIterator last,
                       OutputIterator result, Predicate pred)
{
  while(first!=last)
  {
    if(pred(*first))
        *result++ = *first;
    ++first;
  }
  return result;
}

It's dead easy to write your own:

template <class InputIterator, class OutputIterator, class Predicate>
OutputIterator copy_if(InputIterator first, InputIterator last,
                       OutputIterator result, Predicate pred)
{
  return std::remove_copy_if(first,last,result,std::not1(pred));
}

Edit: This version works with all predicates:

template <class InputIterator, class OutputIterator, class Predicate>
OutputIterator copy_if(InputIterator first, InputIterator last,
                       OutputIterator result, Predicate pred)
{
  while(first!=last)
  {
    if(pred(*first))
        *result++ = *first;
    ++first;
  }
  return result;
}
最好是你 2024-08-12 09:14:23

为了完整起见,我将补充一点,对于那些无法在 boost/algorithm/cxx11/copy_if 中使用 c++11 版本(如我)的人,boost 有 boost::algorithm::copy_if .hpp 将使用 std::copy_if< /a> 时:

#if __cplusplus >= 201103L
//  Use the C++11 versions of copy_if if it is available
using std::copy_if;         // Section 25.3.1
#else

示例:

#include <boost/algorithm/cxx11/copy_if.hpp>
#include <boost/assign/list_of.hpp> // for 'list_of()'
#include <boost/foreach.hpp>

#include <iostream>
#include <vector>
#include <iterator>

struct Odd
{
  bool operator()(int n)
  {
    return n & 1;
  }
};

int main()
{
  std::vector<int> v = boost::assign::list_of(0)(1)(2)(3)(4);
  BOOST_FOREACH(int i, v)
    std::cout << i << ' ' ;

  std::vector<int> out;
  boost::algorithm::copy_if(v.begin(), v.end(), std::back_inserter(out), Odd());

  std::cout << std::endl;

  BOOST_FOREACH(int i, out)
    std::cout << i << ' ' ;

}

输出:

0 1 2 3 4 
1 3 

Just for completeness I will add that boost has boost::algorithm::copy_if for those of you who cannot use c++11's version (like me) in boost/algorithm/cxx11/copy_if.hpp which will use std::copy_if when:

#if __cplusplus >= 201103L
//  Use the C++11 versions of copy_if if it is available
using std::copy_if;         // Section 25.3.1
#else

Example:

#include <boost/algorithm/cxx11/copy_if.hpp>
#include <boost/assign/list_of.hpp> // for 'list_of()'
#include <boost/foreach.hpp>

#include <iostream>
#include <vector>
#include <iterator>

struct Odd
{
  bool operator()(int n)
  {
    return n & 1;
  }
};

int main()
{
  std::vector<int> v = boost::assign::list_of(0)(1)(2)(3)(4);
  BOOST_FOREACH(int i, v)
    std::cout << i << ' ' ;

  std::vector<int> out;
  boost::algorithm::copy_if(v.begin(), v.end(), std::back_inserter(out), Odd());

  std::cout << std::endl;

  BOOST_FOREACH(int i, out)
    std::cout << i << ' ' ;

}

Output:

0 1 2 3 4 
1 3 
九歌凝 2024-08-12 09:14:22

根据 Stroustrup 的《C++ 编程语言》,这只是一个疏忽。

(作为引用,在 boost 邮件列表中回答了同样的问题: copy_if< /a>)

According to Stroustrup's "The C++ Programming Language" it was just an over-sight.

(as a citation, the same question answered in boost mail-lists: copy_if)

小嗲 2024-08-12 09:14:22

斯特鲁斯特鲁普说他们忘记了。它是用 C++11 编写的。

但是,您可以使用 remove_copy_if (实际上应该称为 copy_if_not)和 not1 来代替。

Stroustrup says they forgot it. It's in C++11.

However, you can use remove_copy_if (which really should be called copy_if_not) along with not1 instead.

十雾 2024-08-12 09:14:22

为了完整起见,如果有人用谷歌搜索这个问题,应该提到现在(C++11 之后)一个 复制 if 算法。它的行为符合预期(将某个范围内的某些谓词返回 true 的元素复制到另一个范围)。

一个典型的用例是

std::vector<int> foo{ 25, 15, 5, -5, -15 };
std::vector<int> bar;

// copy only positive numbers:
auto it = std::copy_if (foo.begin(), foo.end(), std::back_inserter(bar), 
            [](int i){return !(i<0);
          });

Just for completeness, in case someone googles his/her way to this question, it should be mentioned that now (post C++11) there is a copy if algorithm. It behaves as expected (copies the elements in a range, for which some predicate returns true, to another range).

A typical use case would be

std::vector<int> foo{ 25, 15, 5, -5, -15 };
std::vector<int> bar;

// copy only positive numbers:
auto it = std::copy_if (foo.begin(), foo.end(), std::back_inserter(bar), 
            [](int i){return !(i<0);
          });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文