• Effective_STL 学习笔记(三十六) 了解 copy_if 的正确 实现


    STL 提供了 11 个名字带有 “copy” 的算法

        copy        copy_backward

        replace_copy     reverse_copy

        replace_copy_if      unique_copy

        remove_copy    rotate_copy

        remove_copy_if   partial_sort_copy

        unintialized_copy

    没有一个是 copy_if 的算法。。。

    想要实现把一个 vector 中所有的有缺陷的 Widget 写到 cerr(如果存在 copy_if):

    1   bool isDefective( const Widget & w );  // 判断 Widget 是否有缺陷
    2   vector<Widget> widgets;
    3   . . . 
    4   copy_if( widgets.begin(), widgets.end(), ostream_iterator<Widget>(cerr, "
    "), isDefective );
    5           // 不能编译,因为 STL 中没有 copy_if 算法

    copy_if 正确的微不足道的实现:

     1   template< typename InputIterator, typename OutputIterator, typename Predicate >
     2   OutputIterator copy_if( InputIterator begin, InputIterator end, 
     3                   OutputIterator destBegin, Predicate p )
     4   {
     5     while( begin != end )
     6     {
     7       if( p(*begin) )
     8         *destBegin++ = *begin;
     9       ++begin;
    10     }  
    11     return destBegin;
    12   }

    copy_if 非常重要

  • 相关阅读:
    JMeter和JMeterPlugin 下载安装
    Beanshell语法
    逻辑控制器
    常用配置元件
    jmeter结构体系
    正则表达式提取器
    ab 测试工具
    spring cloud_docker
    jmeter+maven
    TFS
  • 原文地址:https://www.cnblogs.com/kidycharon/p/10042592.html
Copyright © 2020-2023  润新知