【Boost】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

namespace boost {
  namespace algorithm {
    template<typename RangeT, typename FinderT>
      iterator_range< typename range_iterator< RangeT >::type >
      find(RangeT &, const FinderT &);
  }
}

template<typename SequenceT, typename FinderT, typename FormatterT>
  void find_format(SequenceT & Input, FinderT Finder, FormatterT Formatter);

namespace boost {
  namespace algorithm {
    template<typename OutputIteratorT, typename RangeT, typename FinderT, typename FormatterT>
      OutputIteratorT
      find_format_copy(OutputIteratorT, const RangeT &, FinderT, FormatterT);
      
    template<typename SequenceT, typename FinderT, typename FormatterT>
      SequenceT find_format_copy(const SequenceT &, FinderT, FormatterT);
      
    template<typename SequenceT, typename FinderT, typename FormatterT>
      void find_format(SequenceT &, FinderT, FormatterT);
      
    template<typename OutputIteratorT, typename RangeT, typename FinderT, typename FormatterT>
      OutputIteratorT
      find_format_all_copy(OutputIteratorT, const RangeT &, FinderT, FormatterT);
      
    template<typename SequenceT, typename FinderT, typename FormatterT>
      SequenceT find_format_all_copy(const SequenceT &, FinderT, FormatterT);
      
    template<typename SequenceT, typename FinderT, typename FormatterT>
      void find_format_all(SequenceT &, FinderT, FormatterT);
  }
}

template<typename SequenceT, typename FinderT, typename FormatterT>
  void find_format_all(SequenceT & Input, FinderT Finder, FormatterT Formatter);

template<typename OutputIteratorT, typename RangeT, typename FinderT, typename FormatterT>
  OutputIteratorT
  find_format_all_copy(OutputIteratorT Output, const RangeT & Input, 
                       FinderT Finder, FormatterT Formatter);
                       
template<typename SequenceT, typename FinderT, typename FormatterT>
  SequenceT 
  find_format_all_copy(const SequenceT & Input, FinderT Finder,
                       FormatterT Formatter);

namespace boost {
  namespace algorithm {
    template<typename IteratorT> class find_iterator;
    template<typename IteratorT> class split_iterator;
    template<typename RangeT, typename FinderT>
      find_iterator< typename range_iterator< RangeT >::type >
      make_find_iterator(RangeT &, FinderT);
    template<typename RangeT, typename FinderT>
      split_iterator< typename range_iterator< RangeT >::type >
      make_split_iterator(RangeT &, FinderT);
  }
}

template<typename IteratorT>
class find_iterator {
public:
  // construct/copy/destruct
  find_iterator();
  find_iterator(const find_iterator &);
  template<typename FinderT> find_iterator(IteratorT, IteratorT, FinderT);
  template<typename FinderT, typename RangeT> find_iterator(RangeT &, FinderT);
  // public member functions
  bool eof() const;
  // private member functions
  const match_type & dereference() const;
  void increment();
  bool equal(const find_iterator &) const;
};

template<typename IteratorT>
class split_iterator {
public:
  // construct/copy/destruct
  split_iterator();
  split_iterator(const split_iterator &);
  template<typename FinderT> split_iterator(IteratorT, IteratorT, FinderT);
  template<typename FinderT, typename RangeT>
    split_iterator(RangeT &, FinderT);
  // public member functions
  bool eof() const;
  // private member functions
  const match_type & dereference() const;
  void increment();
  bool equal(const split_iterator &) const;
};

template<typename SequenceSequenceT, typename RangeT, typename FinderT>
  SequenceSequenceT &
  iter_find(SequenceSequenceT & Result, RangeT & Input, FinderT Finder);

template<typename SequenceSequenceT, typename RangeT, typename FinderT>
  SequenceSequenceT &
  iter_split(SequenceSequenceT & Result, RangeT & Input, FinderT Finder);
在find_iterator与split_iterator上运用的例子
void test_string_finder()
{
	using namespace boost;

	// 在find函数中使用
	std::string strSrc1 = "This is a chair, and that is a desk.";
	iterator_range<std::string::iterator> ir = find(strSrc1, first_finder("is"));

	// make_find_iterator, find_iterator
	find_iterator<std::string::iterator> fi1 = make_find_iterator(strSrc1, first_finder("is"));
	find_iterator<std::string::iterator> fi2 = find_iterator<std::string::iterator>(strSrc1, first_finder("is"));
	// 有三个is
	for (fi1; !fi1.eof(); ++fi1)
	{
		std::cout << *fi1 << std::endl;
	}

	// make_split_iterator, split_iterator
	split_iterator<std::string::iterator> si1 = make_split_iterator(strSrc1, first_finder("is"));
	split_iterator<std::string::iterator> si2 = split_iterator<std::string::iterator>(strSrc1, first_finder("is"));
	for (si1; !si1.eof(); ++si1)
	{
		std::string str = copy_range<std::string>(*si1);
		std::cout << *si1 << ": " << str.length() << std::endl;
	}
}



你可能感兴趣的:(【Boost】boost::string_algo详解3——finder的简单应用)