网站建设与管理专业就业,网站建设用图,网站建设一般多少费用,中国建设银行网站签名通下载安装基本概念这里不解释了#xff0c;代码中详细解释了BOOST程序库中对于字符串每一个方法的详细用法#xff1a;
注意#xff1a;这里每实践一个方法#xff0c;都将上面实践过的方法进行了注释#xff0c;如果全部取消注释#xff0c;会出现重命名的问题。
#include …基本概念这里不解释了代码中详细解释了BOOST程序库中对于字符串每一个方法的详细用法
注意这里每实践一个方法都将上面实践过的方法进行了注释如果全部取消注释会出现重命名的问题。
#include iostream
#include string//boost类型转换头文件
#include boost/lexical_cast.hpp
//boost格式化字符头文件
#include boost/format.hpp
//boost轻量化字符串头文件
#include boost/utility/string_ref.hpp
//字符串算法头文件
#include boost/algorithm/string.hppint main() {/*//C自带的字符串转换int ivalue std::stoi(42);int lvalue std::stol(122);long long llvalue std::stoll(42222);double dbvalue std::stod(42.222);std::string szStr std::to_string(12138);std::cout typeid(ivalue).name() \t typeid(lvalue).name() \t typeid(llvalue).name() \t typeid(dbvalue).name() \t typeid(szStr).name() std::endl;std::cout ivalue \t lvalue \t llvalue \t dbvalue \t szStr std::endl;//输出int int __int64 double class std::basic_stringchar,struct std::char_traitschar,class std::allocatorchar //42 122 42222 42.222 12138//boost程序库的转换int nValue boost::lexical_castint (123);long lValue boost::lexical_castlong(123456);long long llValue boost::lexical_castlong long(123456789);double dbValue boost::lexical_castdouble (12.123456);bool bValue boost::lexical_castbool (1);std::string Str boost::lexical_caststd::string (123456789);std::cout typeid(nValue).name() \t typeid(lValue).name() \t typeid(llValue).name() \t typeid(dbValue).name() \t typeid(bValue).name() \t typeid(Str).name() std::endl;std::cout nValue \t lValue \t llValue \t dbValue \t bValue \t Str std::endl;//输出int long __int64 double class std::basic_stringchar,struct std::char_traitschar,class std::allocatorchar //123 123456 123456789 42.222 123456789//使用bosot转换的时候如果转化失败会抛出异常try {int nValue boost::lexical_castint (123);long lValue boost::lexical_castlong(123456);long long llValue boost::lexical_castlong long(123456789);double dbValue boost::lexical_castdouble (12.123456);bool bValue boost::lexical_castbool (123);std::string Str boost::lexical_caststd::string (123456789);std::cout typeid(nValue).name() \t typeid(lValue).name() \t typeid(llValue).name() \t typeid(dbValue).name() \t typeid(bValue).name() \t typeid(Str).name() std::endl;std::cout nValue \t lValue \t llValue \t dbValue \t bValue \t Str std::endl;}catch (boost::bad_lexical_cast e) {std::cout error \t e.what() std::endl;}//输出error bad lexical cast: source type value could not be interpreted as target//也可以使用不抛出异常的方式使用返回的bool来判断是否转换成功int nNum;boost::conversion::try_lexical_convert(123456, nNum);std::cout nNum std::endl;boost::format fmt(%s-%d-%d-%d);std::cout fmt % fdjskalf;djska % 2023 % 05 % 24std::endl;//输出fdjskalf;djska-2023-5-24//也可以使用匿名对象的方式std::cout boost::format(%s-%d-%d-%d) % format % 2023 % 05 % 31 std::endl;//输出format-2023-5-31//实际上底层进行了二次封装basic_formatboost::basic_formatchar bfmt(%s-%d-%d-%d);std::cout bfmt % bfmit % 222 % 111 % 000 std::endl;//输出bfmit-222-111-0//boost中的轻量化字符串//C本身在进行字符串的操作的时候会有一定的成本问题比如说const char* str This is a string;std::string Str(str); //在这个过程中存在一次string类的深拷贝过程//使用boost轻量化字符串也可以使用赋值的方法注意这里是字符串的引用所以不能修改原值boost::string_ref S1(str);boost::string_ref S2(Str);std::cout S1 \t S2 std::endl; //输出This is a string This is a string//可以指定字符个数boost::string_ref S3(str, 4);std::cout S3 std::endl; //输出This//字符串的判断if (S3S1.substr(0,4)) {std::cout std::endl;}//可以在任意位置取出字符std::cout S1.front() std::endl; //Tstd::cout S1.at(2) std::endl; //istd::cout S1.back() std::endl; //g//含有迭代器for (auto i S1.begin(); i ! S1.end(); i) {std::cout *i;}//This is a stringstd::cout std::endl;//字符串的比对这个方法如同strcmp wcscmp返回0代表比对成功std::cout S1.compare(S2) std::endl;//可以取出原始字符串const char* p S1.data();std::cout p std::endl; //This is a string//查找是否存在字串auto a S1.find(is);std::cout typeid(S1.find(is)).name() std::endl; //unsigned intstd::cout a std::endl; //返回第一次出现的位置//对字符串的判断/等操作头文件boost/algorithm/string.hppstd::string Str readmi.txt;//判断文件格式也就是判断字符串尾if (boost::ends_with(Str, .txt)) {std::cout txt文件 std::endl;}//判断头部if (boost::starts_with(Str, rea)) {std::cout rea std::endl;}//判断一个字符是否包含另一个字符串if (boost::contains(Str, read)) {std::cout contains std::endl;}//对字符串进行大小写转换boost::to_upper(Str);std::cout Str std::endl; //READMI.TXTboost::to_lower(Str);std::cout Str std::endl; //readmi.txt//替换字符replace_first:替换第一次出现的/replace_last:替换最后一次出现的/replace_all:替换所有std::string Str readmeread.txt;//boost::replace_first(Str, read, MMMM);//std::cout Str std::endl; //MMMMmeread.txt//boost::replace_last(Str, read, MMMM);//std::cout Str std::endl; //readmeMMMM.txtboost::replace_all(Str, read, MMMM);std::cout Str std::endl; //MMMMmeMMMM.txt//空格删除//1. 删除所有空格std::string Str abcde ;//boost::trim(Str);std::cout Str std::endl; //abcde//2. 删除左边的空格//boost::trim_left(Str); //abcde astd::cout Str a std::endl;//3. 删除右边的空格boost::trim_right(Str);std::cout Str a std::endl; //abcdea*///字符串的查找std::string Str readreadreadmereadreadread.txt;std::cout typeid(boost::find_first(Str, read)).name() std::endl; //返回值类型class boost::iterator_rangeclass std::_String_iteratorclass std::_String_valstruct std::_Simple_typeschar auto a boost::find_first(Str, read);std::cout a std::endl; //read//boost::find_last方法取出最后出现的//从字符串中取出字符//取出头部6字节auto head boost::find_head(Str, 6);std::cout typeid(boost::find_head(Str, 6)).name() std::endl; //class boost::iterator_rangeclass std::_String_iteratorclass std::_String_valstruct std::_Simple_typeschar std::cout head std::endl; //readre//取出尾部6字节auto tail boost::find_tail(Str, 6);std::cout typeid(boost::find_tail(Str, 6)).name() std::endl; //class boost::iterator_rangeclass std::_String_iteratorclass std::_String_valstruct std::_Simple_typeschar std::cout tail std::endl; //ad.txtreturn 0;
}如果发现文章中有错误还请大家指出来我会非常虚心地学习我们一起进步 文章转载自: http://www.morning.jcypk.cn.gov.cn.jcypk.cn http://www.morning.lwmxk.cn.gov.cn.lwmxk.cn http://www.morning.yrkdq.cn.gov.cn.yrkdq.cn http://www.morning.hrkth.cn.gov.cn.hrkth.cn http://www.morning.diuchai.com.gov.cn.diuchai.com http://www.morning.pdtjj.cn.gov.cn.pdtjj.cn http://www.morning.rqjxc.cn.gov.cn.rqjxc.cn http://www.morning.lbcbq.cn.gov.cn.lbcbq.cn http://www.morning.fmjzl.cn.gov.cn.fmjzl.cn http://www.morning.qczpf.cn.gov.cn.qczpf.cn http://www.morning.zlgr.cn.gov.cn.zlgr.cn http://www.morning.rgxf.cn.gov.cn.rgxf.cn http://www.morning.nlgnk.cn.gov.cn.nlgnk.cn http://www.morning.hhboyus.cn.gov.cn.hhboyus.cn http://www.morning.fcxt.cn.gov.cn.fcxt.cn http://www.morning.brlgf.cn.gov.cn.brlgf.cn http://www.morning.brbmf.cn.gov.cn.brbmf.cn http://www.morning.btqqh.cn.gov.cn.btqqh.cn http://www.morning.kqzxk.cn.gov.cn.kqzxk.cn http://www.morning.kbntl.cn.gov.cn.kbntl.cn http://www.morning.hyyxsc.cn.gov.cn.hyyxsc.cn http://www.morning.ghzfx.cn.gov.cn.ghzfx.cn http://www.morning.kfhm.cn.gov.cn.kfhm.cn http://www.morning.smhtg.cn.gov.cn.smhtg.cn http://www.morning.gkxyy.cn.gov.cn.gkxyy.cn http://www.morning.lqqqh.cn.gov.cn.lqqqh.cn http://www.morning.lslin.com.gov.cn.lslin.com http://www.morning.yrdkl.cn.gov.cn.yrdkl.cn http://www.morning.rwhlf.cn.gov.cn.rwhlf.cn http://www.morning.tqpds.cn.gov.cn.tqpds.cn http://www.morning.ggnrt.cn.gov.cn.ggnrt.cn http://www.morning.jbmsp.cn.gov.cn.jbmsp.cn http://www.morning.kqpxb.cn.gov.cn.kqpxb.cn http://www.morning.lzttq.cn.gov.cn.lzttq.cn http://www.morning.bgqqr.cn.gov.cn.bgqqr.cn http://www.morning.kkjlz.cn.gov.cn.kkjlz.cn http://www.morning.rkwlg.cn.gov.cn.rkwlg.cn http://www.morning.dhtdl.cn.gov.cn.dhtdl.cn http://www.morning.ccjhr.cn.gov.cn.ccjhr.cn http://www.morning.yktwr.cn.gov.cn.yktwr.cn http://www.morning.mdmc.cn.gov.cn.mdmc.cn http://www.morning.mnsts.cn.gov.cn.mnsts.cn http://www.morning.jbhhj.cn.gov.cn.jbhhj.cn http://www.morning.ampingdu.com.gov.cn.ampingdu.com http://www.morning.mrccd.cn.gov.cn.mrccd.cn http://www.morning.nqmdc.cn.gov.cn.nqmdc.cn http://www.morning.ksggr.cn.gov.cn.ksggr.cn http://www.morning.frfpx.cn.gov.cn.frfpx.cn http://www.morning.lkbkd.cn.gov.cn.lkbkd.cn http://www.morning.klyyd.cn.gov.cn.klyyd.cn http://www.morning.bwjws.cn.gov.cn.bwjws.cn http://www.morning.xpmwt.cn.gov.cn.xpmwt.cn http://www.morning.jrplk.cn.gov.cn.jrplk.cn http://www.morning.wtrjq.cn.gov.cn.wtrjq.cn http://www.morning.shangwenchao4.cn.gov.cn.shangwenchao4.cn http://www.morning.hbqhz.cn.gov.cn.hbqhz.cn http://www.morning.hybmz.cn.gov.cn.hybmz.cn http://www.morning.smrty.cn.gov.cn.smrty.cn http://www.morning.flfdm.cn.gov.cn.flfdm.cn http://www.morning.dbjyb.cn.gov.cn.dbjyb.cn http://www.morning.pmrlt.cn.gov.cn.pmrlt.cn http://www.morning.trrrm.cn.gov.cn.trrrm.cn http://www.morning.rcyrm.cn.gov.cn.rcyrm.cn http://www.morning.dwyyf.cn.gov.cn.dwyyf.cn http://www.morning.qpnmd.cn.gov.cn.qpnmd.cn http://www.morning.ntzfl.cn.gov.cn.ntzfl.cn http://www.morning.mnqz.cn.gov.cn.mnqz.cn http://www.morning.dlbpn.cn.gov.cn.dlbpn.cn http://www.morning.yggdq.cn.gov.cn.yggdq.cn http://www.morning.kdlzz.cn.gov.cn.kdlzz.cn http://www.morning.nngq.cn.gov.cn.nngq.cn http://www.morning.myfwb.cn.gov.cn.myfwb.cn http://www.morning.hrydl.cn.gov.cn.hrydl.cn http://www.morning.rscrj.cn.gov.cn.rscrj.cn http://www.morning.msbmp.cn.gov.cn.msbmp.cn http://www.morning.xnwjt.cn.gov.cn.xnwjt.cn http://www.morning.mhrzd.cn.gov.cn.mhrzd.cn http://www.morning.xhklb.cn.gov.cn.xhklb.cn http://www.morning.ybgyz.cn.gov.cn.ybgyz.cn http://www.morning.czwed.com.gov.cn.czwed.com