游戏开发论坛

 找回密码
 立即注册
搜索
查看: 4875|回复: 0

boost之2.字符串算法

[复制链接]

56

主题

94

帖子

98

积分

注册会员

Rank: 2

积分
98
发表于 2010-8-14 20:47:00 | 显示全部楼层 |阅读模式
本文主要涉及boost中algorithm之string问题
基本的字符串函数类型有
替换
裁剪
大小写替换
正则表达式
切割
判断
和擦除操作等等
//! boost之2:字符串算法类
#include <string>
#include <vector>
#include <iostream>
#include <iterator>
#include <functional>
#include <boost/algorithm/string.hpp>
using
namespace std;
using
namespace boost;

int main()
{  
    //! 字符串

string str(" abc-*-ABC-*-aBc ");
   
    //! 以-或者*切割字符串
    vector<std::string> ret;
    split(ret,str,is_any_of("-*"),token_compress_on);

    for(unsigned int index=0;index<ret.size();index++)
    {
        cout<<index<<":"<<ret[index]<<endl;
    };
   
    //! 切换为小写
    to_lower(str);
    cout<<"*"<<str<<"*"<<endl;
    //! 去掉左边的空格
    trim_left(str);
    cout<<"*"<<str<<"*"<<endl;
    //! 去掉右边的空格
    trim_right(str);
    cout<<"*"<<str<<"*"<<endl;
   
    //! 替换
    replace_all(str,"a","A");
    cout<<str<<endl;
    //! 擦除
    cout<<erase_all_copy(str,"A")<<endl;
   
    replace_nth(str,"c",2,"ccsdu2004");
    cout<<str<<endl;
   
    //! 擦除给定范围
    cout<<erase_range_copy(str,make_iterator_range(str.begin()+2,str.begin()+5))<<endl;
   
    cout<<"is start with:A:"<<starts_with(str,string("A"))<<endl;
    cout<<"is end with:C:"<<ends_with(str,string("C"))<<endl;
    cout<<"is contain with:ccs:"<<contains(str,string("ccs"))<<endl;
   
    cout<<endl;
    system(&quotAUSE");
    return
0;
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-3-19 16:15

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表