Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿
本文主要涉及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(
"PAUSE");
    
return 0;
}


posted on 2010-08-10 10:11 ccsdu2009 阅读(623) 评论(0)  编辑 收藏 引用 所属分类: boost库等

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理