Boost之string处理篇
#include <iostream>
#include <string>
#include <vector>
#include <boost\lexical_cast.hpp>
#include <boost\algorithm\string.hpp>
using namespace std;
using namespace boost;
void string_trans_other(); // int、double、string类型的数据转换
void string_handler_sub(); //结尾判断、string大小写转换、替换某个字符串
void enum_handler_sub(); //枚举处理
int main()
{
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>//string_trans_other();
<span style="white-space:pre"> </span>string_handler_sub();
<span style="white-space:pre"> </span>//enum_handler_sub();
<span style="white-space:pre"> </span>getchar();
return 0;
}
void string_trans_other()
{
<span style="white-space:pre"> </span>int a = lexical_cast<int>("123"); //从string---int
<span style="white-space:pre"> </span>double b = lexical_cast<double>("123.23"); //从string---double
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>const double d = 123.33;
<span style="white-space:pre"> </span>string ss = lexical_cast<string>(d);
}
void string_handler_sub()
{
<span style="white-space:pre"> </span>string str_aa("11readme.txt");
<span style="white-space:pre"> </span>if(ends_with(str_aa,"txt")) //ends_with判断一个字符串是不是以什么结尾
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>to_upper(str_aa); //to_upper(string) 变为大写
<span style="white-space:pre"> </span>cout<<"1:"<<str_aa<<endl;
<span style="white-space:pre"> </span>string str_aa("readme.txt");
<span style="white-space:pre"> </span>cout<<"2:"<<to_upper_copy(str_aa)+"UPPER"<<endl;
<span style="white-space:pre"> </span>assert(ends_with(str_aa,"txt"));
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>replace_first(str_aa,"README","followme");//replace_first替换功能
<span style="white-space:pre"> </span>cout<<"3:"<<str_aa<<endl;
<span style="white-space:pre"> </span>vector<char> char_v(str_aa.begin(),str_aa.end());
<span style="white-space:pre"> </span>vector<char> char_v2 = to_upper_copy(
<span style="white-space:pre"> </span>erase_first_copy(char_v,"txt"));
<span style="white-space:pre"> </span>for (int i =0 ; i < char_v2.size(); ++i)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>cout<<char_v2[i];
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>string str_bb ("I love you .");
<span style="white-space:pre"> </span>string str_bb_up = to_upper_copy(str_bb);//全部转成大写
<span style="white-space:pre"> </span>string str_bb_low = to_lower_copy(str_bb);//全部转成小写
<span style="white-space:pre"> </span>
}
void enum_handler_sub()
{
<span style="white-space:pre"> </span>enum room_brother_name {
<span style="white-space:pre"> </span>Xuxin = 1,
<span style="white-space:pre"> </span>Xujingli = 2,
<span style="white-space:pre"> </span>Liuzibin = 3,
<span style="white-space:pre"> </span>Jiyanghongkang = 4
<span style="white-space:pre"> </span>}wh;
<span style="white-space:pre"> </span>wh = Xuxin; //定义一个枚举
<span style="white-space:pre"> </span>cout<<"the value is :"<<wh<<endl;
}