zoukankan      html  css  js  c++  java
  • 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 非常重要

  • 相关阅读:
    http和HTTPS的区别及SSL介绍
    cas系列(三)--HTTP和HTTPS、SSL
    cas系列(一)--cas单点登录基本原理
    修改cas登陆页面-服务器端
    rest例子
    RESTful 架构风格概述
    Java分布式应用技术架构介绍
    tomcat的server.xml详解
    Apache Shiro 快速入门教程,shiro 基础教程 (这篇文章非常好)
    fmt:formatDate标签的输出格式
  • 原文地址:https://www.cnblogs.com/kidycharon/p/10042592.html
Copyright © 2011-2022 走看看