boost 字符串,json
来源:互联网 发布:小心眼表现 知乎 编辑:程序博客网 时间:2024/05/16 05:27
转换
#include <iostream>#include <boost/bind.hpp>#include <boost/date_time/posix_time/posix_time.hpp>#include <boost/lexical_cast.hpp>using namespace boost;int main(){ try{int x=lexical_cast<int>("100");float f=lexical_cast<float>("100.123456");printf("f=%f\n",f);std::string str=lexical_cast<std::string>(12.365);std::cout<<str<<std::endl;str=lexical_cast<std::string>(0x10);std::cout<<str<<std::endl; } catch(bad_lexical_cast& e){ std::cout << e.what() << std::endl; }}
f=100.12345912.36516
格式化
#include <iostream>#include <boost/bind.hpp>#include <boost/date_time/posix_time/posix_time.hpp>#include <boost/format.hpp>#include <boost/format/exceptions.hpp>using namespace boost;int main(){ try{ std::cout<<format("%s:%d,%d")%"hello"%123%999<<std::endl; format fmt("%1% %2%,%2%, %3%");//%n%标记第n个参数 fmt%123%999%"hello"; std::cout<<fmt.str()<<std::endl; format fmt2("%10d %.2f");//此种与printf类似的写法 fmt2%66%2.567; std::cout<<fmt2.str()<<std::endl; } catch(io::format_error& e){ std::cout << e.what() << std::endl; }}
hello:123,999123 999,999, hello 66 2.57
判断
#include <iostream>#include <boost/bind.hpp>#include <boost/date_time/posix_time/posix_time.hpp>#include <boost/algorithm/string.hpp>using namespace boost;int main(){ try{ std::string str("Power Bomb"); assert(iends_with(str,"Bomb")); assert(!ends_with(str,"bomb")); assert(istarts_with(str,"Power")); assert(!starts_with(str,"power")); assert(contains(str,"er")); assert(!contains(str,"Er")); assert(equals(str,"Power Bomb")); assert(!equals(str,"power Bomb")); } catch(std::exception& e){ std::cout << e.what() << std::endl; }}
修剪
#include <iostream>#include <boost/bind.hpp>#include <boost/date_time/posix_time/posix_time.hpp>#include <boost/algorithm/string.hpp>#include <boost/format.hpp>using namespace boost;int main(){format fmt("|%s|\n"); try{ std::string str(" Power Bomb "); std::cout<<fmt%trim_left_copy(str); std::cout<<fmt%trim_right_copy(str); std::cout<<fmt%trim_copy(str); } catch(std::exception& e){ std::cout << e.what() << std::endl; }}
|Power Bomb || Power Bomb||Power Bomb|
查找
#include <iostream>#include <boost/bind.hpp>#include <boost/date_time/posix_time/posix_time.hpp>#include <boost/format.hpp>#include <boost/algorithm/string.hpp>using namespace boost;int main(){ try{ format fmt("|%s|.pos=%d\n"); std::string str = "Long Long ago , there is a girl"; iterator_range<std::string::iterator> rge; rge=find_first(str,"long"); std::cout<<fmt%rge%(rge.begin()-str.begin()); rge=ifind_first(str,"long"); std::cout<<fmt%rge%(rge.begin()-str.begin()); } catch(io::format_error& e){ std::cout << e.what() << std::endl; }}
||.pos=31|Long|.pos=0
替换和删除
#include <iostream>#include <boost/bind.hpp>#include <boost/date_time/posix_time/posix_time.hpp>#include <boost/algorithm/string.hpp>using namespace boost;int main(){ try{ std::string str = "Samus and Samus fuck the pig\n"; std::cout<<replace_all_copy(str,"Samus","jim"); std::cout<<replace_first_copy(str,"Samus","jim"); std::cout<<replace_last_copy(str,"Samus","jim"); std::cout<<replace_tail_copy(str,1,"Samus\n"); std::cout<<erase_last_copy(str,"Samus"); std::cout<<erase_tail_copy(str,2); std::cout<<erase_head_copy(str,2); } catch(std::exception& e){ std::cout << e.what() << std::endl; }}
jim and jim fuck the pigjim and Samus fuck the pigSamus and jim fuck the pigSamus and Samus fuck the pigSamusSamus and fuck the pigSamus and Samus fuck the pimus and Samus fuck the pig分割
#include <iostream>#include <deque>#include <boost/bind.hpp>#include <boost/date_time/posix_time/posix_time.hpp>#include <boost/algorithm/string.hpp>using namespace boost;int main(){ try{std::string str = "Samus,and,Samus fuck the,pig";std::deque<std::string> d;split(d,str,is_any_of(","),token_compress_on);for(std::deque<std::string>::iterator it=d.begin();it!=d.end();it++){std::cout<<(*it)<<std::endl;} } catch(std::exception& e){ std::cout << e.what() << std::endl; }}
SamusandSamus fuck thepig
解析json
a.json
{"server":[{"type":"tcp","ip":"1.2.3.4","port":"8088"},{"type":"tcp","ip":"2.2.3.5","port":"9900"}],"trace":[{"type":"udp","ip":"192.168.1.116","port":"9988"}],"info":[{"id":"1234566","pswd":"12345678","version":"0.9"}],"pattern":"LUCE","pattern2":"XIANKAN"}
#include <iostream>#include <boost/property_tree/ptree.hpp>#include <boost/property_tree/json_parser.hpp>#include <boost/foreach.hpp>int ParseJson(){int fd = open("a.json", O_RDONLY);if (fd < 0) {DBG("open zed_WXCJ.conf err");return false;}char buf[1024]={0};int len = read(fd, buf, sizeof(buf));if(len<=0)return false;std::string str=buf;using namespace boost::property_tree;std::stringstream ss(str);ptree pt;try{read_json(ss, pt);}catch(ptree_error & e) {printf("%s\n",e.what());} try{ std::string sPt = pt.get<std::string>("pattern"); std::cout<<sPt<<std::endl; ptree image_array = pt.get_child("server"); BOOST_FOREACH(boost::property_tree::ptree::value_type &v, image_array) { std::string s2 = v.second.get<std::string>("ip"); int port = v.second.get<int>("port"); std::cout<<s2<<":"<<port<<std::endl; } image_array = pt.get_child("info"); BOOST_FOREACH(boost::property_tree::ptree::value_type &v, image_array) { std::string sId = v.second.get<std::string>("id"); std::string sPswd = v.second.get<std::string>("pswd"); std::string sVersion = v.second.get<std::string>("version"); std::cout<<sId<<":"<<sPswd<<":"<<sVersion<<std::endl; } } catch (ptree_error & e) { printf("%s\n",e.what()); } return 0;}int main(){ParseJson();}
0 0
- boost 字符串,json
- Boost ptree 解析json字符串 多线程下程序crash
- 用boost的PropertyTree json_parser 生成json字符串
- Boost ptree 解析json字符串 多线程下程序crash
- Boost解析JSON格式
- boost 访问JSON
- 利用BOOST,解释JSON
- Boost之Json解析
- BOOST JSON 数组解析
- boost解析json文件
- boost库 json解析
- C++ Boost 解析JSON
- C++ boost 解析 Json
- boost的字符串算法
- boost 字符串操作
- boost字符串算法
- BOOST 字符串查找示例
- boost 处理字符串
- Android APK反编译详解(附图)
- Android中的设计模式
- boost thread
- 二叉树小结
- BZOJ-3171-循环格-TJOI2013-费用流
- boost 字符串,json
- Marked Ancestor (AOJ 2170 并查集)
- 杭电 HDU 1038 Biker's Trip Odometer
- boost 指针
- MySQL的JDBC驱动源码解析
- mfc中关于画图操作而导致ASSERT(m_hObject == NULL);中断问题
- HNU13247Connected Caves (拓扑)
- Java中getResourceAsStream的用法
- jdk的安装与配置