boost——字符串与文本处理string_algo(二)

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <boost/algorithm/string.hpp>
#include <boost/typeof/typeof.hpp>
#include <boost/assign.hpp>
#include <list>
using namespace boost;
using namespace std;
using namespace boost::assign;

struct is_contains_a
{
    bool operator()(const string& x)
    {
        return contains(x,"a");  
    }       
};
int main( int argc,char **argv)
{
    /*分割*/
    string str = "Sam,ZeLinDa,us,Li+nk,Zelinda::Mario,L.inui-wudiab";
    cout << str << endl;
    deque<string> d;
    ifind_all(d,str,"zelinDa");
    assert(d.size() == 2);
    for(BOOST_AUTO(pos,d.begin());pos != d.end();++pos){
         cout << "["<< *pos <<"] " ;
     }
    cout << endl;

    list<iterator_range<string::iterator> > ilist;
    split(ilist,str,is_any_of(",.:-+"));
    for(BOOST_AUTO(pos,ilist.begin());pos != ilist.end();++pos){
         cout << "["<< *pos <<"] " ;
    } 
    cout << endl;

    ilist.clear();
    split(ilist,str,is_any_of(".:-"),token_compress_on);    //参数eCompress可以取值为token_compress_on或token_compress_off,如果值为前者,那么两个分隔符被视为一个,若为后者则连续分隔符标记了一个空字符串,默认为后者。
    for(BOOST_AUTO(pos,ilist.begin());pos != ilist.end();++pos){
         cout << "["<< *pos <<"] " ;
    } 
    cout << endl;

    /*合并*/
    vector<string> v = list_of("Tony")("Link")("Ton")("join");
    cout << join(v,"+") <<endl; //合并
    cout << join_if(v,"**",is_contains_a());    //合并

    /*查找分割迭代器*/
    string str12("Samus||samus||mario|||linik");

    typedef find_iterator<string::iterator> string_find_iterator;;
    string_find_iterator pos,end;
    for(pos  = make_find_iterator(str12,first_finder("samus",is_iequal()));pos != end;++pos)
    {
        cout <<"[" << *pos << "] ";
    }
    cout << endl;

    typedef split_iterator<string::iterator> string_split_iterator;
    string_split_iterator p,endp;
    for(p = make_split_iterator(str12,first_finder("||",is_equal()));p != endp;++p)
    {
         cout << "[" << *p << "] ";
    }
    cout << endl;

     return (0);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值