boost::string_algo详解3——finder的简单应用

Finder是一个搜索某个容器的任意部分的仿函数,一般无法单独使用。搜索的结果以一个限定所选择的部分的iterator_range的形式给出。常用于函数
find, 
find_format, find_format_copy, 
find_format_all, find_format_all_copy
find_iterator的工厂方法: make_find_iterator, make_split_iterator
find_iterator的构造函数: find_iterator, split_iterator
iter_find, iter_split

[cpp]   view plain copy print ?
  1. namespace boost {  
  2.   namespace algorithm {  
  3.     template<typename RangeT, typename FinderT>  
  4.       iterator_range< typename range_iterator< RangeT >::type >  
  5.       find(RangeT &, const FinderT &);  
  6.   }  
  7. }  
  8.   
  9. template<typename SequenceT, typename FinderT, typename FormatterT>  
  10.   void find_format(SequenceT & Input, FinderT Finder, FormatterT Formatter);  
  11.   
  12. namespace boost {  
  13.   namespace algorithm {  
  14.     template<typename OutputIteratorT, typename RangeT, typename FinderT, typename FormatterT>  
  15.       OutputIteratorT  
  16.       find_format_copy(OutputIteratorT, const RangeT &, FinderT, FormatterT);  
  17.         
  18.     template<typename SequenceT, typename FinderT, typename FormatterT>  
  19.       SequenceT find_format_copy(const SequenceT &, FinderT, FormatterT);  
  20.         
  21.     template<typename SequenceT, typename FinderT, typename FormatterT>  
  22.       void find_format(SequenceT &, FinderT, FormatterT);  
  23.         
  24.     template<typename OutputIteratorT, typename RangeT, typename FinderT, typename FormatterT>  
  25.       OutputIteratorT  
  26.       find_format_all_copy(OutputIteratorT, const RangeT &, FinderT, FormatterT);  
  27.         
  28.     template<typename SequenceT, typename FinderT, typename FormatterT>  
  29.       SequenceT find_format_all_copy(const SequenceT &, FinderT, FormatterT);  
  30.         
  31.     template<typename SequenceT, typename FinderT, typename FormatterT>  
  32.       void find_format_all(SequenceT &, FinderT, FormatterT);  
  33.   }  
  34. }  
  35.   
  36. template<typename SequenceT, typename FinderT, typename FormatterT>  
  37.   void find_format_all(SequenceT & Input, FinderT Finder, FormatterT Formatter);  
  38.   
  39. template<typename OutputIteratorT, typename RangeT, typename FinderT, typename FormatterT>  
  40.   OutputIteratorT  
  41.   find_format_all_copy(OutputIteratorT Output, const RangeT & Input,   
  42.                        FinderT Finder, FormatterT Formatter);  
  43.                          
  44. template<typename SequenceT, typename FinderT, typename FormatterT>  
  45.   SequenceT   
  46.   find_format_all_copy(const SequenceT & Input, FinderT Finder,  
  47.                        FormatterT Formatter);  
  48.   
  49. namespace boost {  
  50.   namespace algorithm {  
  51.     template<typename IteratorT> class find_iterator;  
  52.     template<typename IteratorT> class split_iterator;  
  53.     template<typename RangeT, typename FinderT>  
  54.       find_iterator< typename range_iterator< RangeT >::type >  
  55.       make_find_iterator(RangeT &, FinderT);  
  56.     template<typename RangeT, typename FinderT>  
  57.       split_iterator< typename range_iterator< RangeT >::type >  
  58.       make_split_iterator(RangeT &, FinderT);  
  59.   }  
  60. }  
  61.   
  62. template<typename IteratorT>  
  63. class find_iterator {  
  64. public:  
  65.   // construct/copy/destruct  
  66.   find_iterator();  
  67.   find_iterator(const find_iterator &);  
  68.   template<typename FinderT> find_iterator(IteratorT, IteratorT, FinderT);  
  69.   template<typename FinderT, typename RangeT> find_iterator(RangeT &, FinderT);  
  70.   // public member functions  
  71.   bool eof() const;  
  72.   // private member functions  
  73.   const match_type & dereference() const;  
  74.   void increment();  
  75.   bool equal(const find_iterator &) const;  
  76. };  
  77.   
  78. template<typename IteratorT>  
  79. class split_iterator {  
  80. public:  
  81.   // construct/copy/destruct  
  82.   split_iterator();  
  83.   split_iterator(const split_iterator &);  
  84.   template<typename FinderT> split_iterator(IteratorT, IteratorT, FinderT);  
  85.   template<typename FinderT, typename RangeT>  
  86.     split_iterator(RangeT &, FinderT);  
  87.   // public member functions  
  88.   bool eof() const;  
  89.   // private member functions  
  90.   const match_type & dereference() const;  
  91.   void increment();  
  92.   bool equal(const split_iterator &) const;  
  93. };  
  94.   
  95. template<typename SequenceSequenceT, typename RangeT, typename FinderT>  
  96.   SequenceSequenceT &  
  97.   iter_find(SequenceSequenceT & Result, RangeT & Input, FinderT Finder);  
  98.   
  99. template<typename SequenceSequenceT, typename RangeT, typename FinderT>  
  100.   SequenceSequenceT &  
  101.   iter_split(SequenceSequenceT & Result, RangeT & Input, FinderT Finder);  
在find_iterator与split_iterator上运用的例子
[cpp]   view plain copy print ?
  1. void test_string_finder()  
  2. {  
  3.     using namespace boost;  
  4.   
  5.     // 在find函数中使用  
  6.     std::string strSrc1 = "This is a chair, and that is a desk.";  
  7.     iterator_range<std::string::iterator> ir = find(strSrc1, first_finder("is"));  
  8.   
  9.     // make_find_iterator, find_iterator  
  10.     find_iterator<std::string::iterator> fi1 = make_find_iterator(strSrc1, first_finder("is"));  
  11.     find_iterator<std::string::iterator> fi2 = find_iterator<std::string::iterator>(strSrc1, first_finder("is"));  
  12.     // 有三个is  
  13.     for (fi1; !fi1.eof(); ++fi1)  
  14.     {  
  15.         std::cout << *fi1 << std::endl;  
  16.     }  
  17.   
  18.     // make_split_iterator, split_iterator  
  19.     split_iterator<std::string::iterator> si1 = make_split_iterator(strSrc1, first_finder("is"));  
  20.     split_iterator<std::string::iterator> si2 = split_iterator<std::string::iterator>(strSrc1, first_finder("is"));  
  21.     for (si1; !si1.eof(); ++si1)  
  22.     {  
  23.         std::string str = copy_range<std::string>(*si1);  
  24.         std::cout << *si1 << ": " << str.length() << std::endl;  
  25.     }  
  26. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值