字符串查找
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
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);
- 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;
- }
- }
- #include <string>
- #include <iostream>
- #include <algorithm>
- #include <functional>
- #include <boost/algorithm/string/case_conv.hpp>
- #include <boost/algorithm/string/find.hpp>
- using namespace std;
- using namespace boost;
- int main()
- {
- cout << "* Find Example *" << endl << endl;
- string str1("abc___cde___efg");
- string str2("abc");
- // find "cde" substring
- iterator_range<string::iterator> range=find_first( str1, string("cde") );
- // convert a substring to upper case
- // note that iterator range can be directly passed to the algorithm
- to_upper( range );
- cout << "str1 with upper-cased part matching cde: " << str1 << endl;
- // get a head of the string
- iterator_range<string::iterator> head=find_head( str1, 3 );
- cout << "head(3) of the str1: " << string( head.begin(), head.end() ) << endl;
- // get the tail
- head=find_tail( str2, 5 );
- cout << "tail(5) of the str2: " << string( head.begin(), head.end() ) << endl;
- // char processing
- char text[]="hello dolly!";
- iterator_range<char*> crange=find_last(text,"ll");
- // transform the range ( add 1 )
- transform( crange.begin(), crange.end(), crange.begin(), bind2nd( plus<char>(), 1 ) );
- // uppercase the range
- to_upper( crange );
- cout << text << endl;
- cout << endl;
- return 0;
- }
字符串替换
1、std::String 方法
#include<string> #include<iostream> using namespace std; //第一种替换字符串的方法用replace() void string_replace(string&s1,const string&s2,const string&s3) { string::size_type pos=0; string::size_type a=s2.size(); string::size_type b=s3.size(); while((pos=s1.find(s2,pos))!=string::npos) { s1.replace(pos,a,s3); pos+=b; } } //第二种替换字符串的方法用erase()和insert() void string_replace_2(string&s1,const string&s2,const string&s3) { string::size_type pos=0; string::size_type a=s2.size(); string::size_type b=s3.size(); while((pos=s1.find(s2,pos))!=string::npos) { s1.erase(pos,a); s1.insert(pos,s3); pos+=b; } }
2、boost库的正则表达式#include <stdio.h>
#include <fstream>
#include <sstream>
#include <string>
#include <iterator>
#include <boost/regex.hpp>
#include <fstream>
#include <iostream>
int main(int argc, char *argv[])
{
printf("Hello, world\n");
std::string in("my is test ! ... ..'");
boost::regex e1("'");
std::string result = boost::regex_replace(in,e1,"\'", boost::match_default | boost::format_all);
std::cout << result << std::endl;
return 0;
}
3、boost库的字符串相关算法
Erase/Replace
boost把erase和replace函数分开来列了,这样的好处是命名比较清晰,但不好的地方时函数变得非常多,不如重载的形式那么好记。replace的函数有replace_all() 、replace_first()、 replace_last() 以及它们的变体,加上erase,共有20多种,这里就不一一列举了。
cout << replace_all_copy(string("hello world"), "l", "-") << endl;