免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2026 | 回复: 4

[C++] [variadic template]泛型 count 的实现 -- C++er 看过来 [复制链接]

论坛徽章:
0
发表于 2012-08-24 03:22 |显示全部楼层
在试图实现一个泛型 count  的时候遇到一些问题,特此请教:

想要实现的 count( 原型示意而已,不合语法,[]内的部分表示可选 )

  1. template<typename Itor1, [typename Itor2, ...., typename ItorN,] typename Value1[, typename Value2, ..., typename ValueN]>
  2. std::size_t count( Itor1 first1, Itor1 last1, [Itor2 first2, ..., ItorN firstN,] Value1 v1[, Value2 v2, ..., ValueN vn] );
复制代码
我已经完成了一个泛型的 count_if,但是使用这个 count_if 实现 count 的时候发现很难把 v1, v2, ..., vn 转化成一个 Prediction 的function object,
请指点。

楼下贴出 count_if 的泛型实现。

论坛徽章:
0
发表于 2012-08-24 03:23 |显示全部楼层
count_if 的泛型实现:
  1. #if 0
  2.     Proto type:
  3.         size_t count_if( Iterator1 first1, Iterator1 last1, [ Iterator2 first2, ..., IteratorN firstN, ] Pred p );
  4.     Return:
  5.         the number of elements in the range satisfying Prediction p
  6. #endif

  7. #include <cstddef>

  8. namespace algorithm
  9. {

  10.     namespace count_if_private
  11.     {

  12.         template< typename Predicate, typename First_Input_Iterator, typename ... Rest_Input_Iterators >
  13.         std::size_t _count_if( Predicate predict, First_Input_Iterator first, First_Input_Iterator last, Rest_Input_Iterators ... rest )
  14.         {
  15.             if ( first == last )
  16.                 return 0;
  17.             return ( predict( *first++, *rest++... ) ? 1 : 0 )  + _count_if( predict, first, last, rest... );
  18.         }

  19.         struct dummy {};

  20.         template<typename Predicate, typename ... Input_Iterators>
  21.         std::size_t rotate_count_if_impl( Predicate predict, dummy, Input_Iterators ... inputs )
  22.         {
  23.             return _count_if( predict, inputs ... );
  24.         }

  25.         template<typename Anonymous_Arg1, typename ... Anonymous_Argn>
  26.         std::size_t rotate_count_if_impl( Anonymous_Arg1 arg1, Anonymous_Argn ... argn )
  27.         {
  28.             return rotate_count_if_impl( argn ..., arg1 );
  29.         }

  30.     }//namespace count_if_private

  31.     template<typename ... Input_Iterators_and_Predict>
  32.     std::size_t count_if( Input_Iterators_and_Predict ... all_args )
  33.     {
  34.         static_assert( sizeof ... ( all_args ) > 2, "algorithm::count_if requires at least 3 arguments" );
  35.         return count_if_private::rotate_count_if_impl( all_args ..., count_if_private::dummy() );
  36.     }

  37. }//namespace algorithm
复制代码

论坛徽章:
0
发表于 2012-08-24 05:55 |显示全部楼层
fallening_cu 发表于 2012-08-24 03:22
在试图实现一个泛型 count  的时候遇到一些问题,特此请教:

想要实现的 count( 原型示意而已,不合语 ...


难点在于
1) 把有 2N +1 个参数的函数中后边 N 个取出做一个 function object,这个 function object 是一个接受 N 个参数的函数,并判断这些参数是否与 count_if 提供的后 N 个参数相等。
2)把前边 N+1 个参数提取出来转发给 count_if
  1. template< typename ... Args>
  2. size_t count( Args ... args )
  3. {
  4.      return count_if( extract_N_1_args( args...), make_predict_functor_with_N_args( args...) );
  5. }
复制代码

论坛徽章:
0
发表于 2012-08-24 19:29 |显示全部楼层
楼主试试大召唤术
@Moon_Bird
@starwing83
@OwnWaterloo
@L_kernel
@__BlueGuy__
@whyglinux
@X-Hawk
@AD8018
@bruceteen
@koolcoy
@幻の上帝
@zylthinking
@chary8088

论坛徽章:
0
发表于 2012-08-24 19:31 |显示全部楼层
楼下诸位,大召唤术怎么用??
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP