#include <boost/lexical_cast.hpp>
void test_lexical_cast()
{
int number = 123;
string str = "456";
try
{
int tmp1 = boost::lexical_cast<int>(str);
string tmp2 = boost::lexical_cast<string>(number);
cout<<tmp1<<endl;
cout<<tmp2<<endl;
}catch(boost::bad_lexical_cast& ex)
{
cout<<ex.what()<<endl;
}
}

//==============================================================================
#include <boost/format.hpp>
void test_format()
{
int number = 123;
string str = "456";
cout<<boost::format("%1%:%2%") % number % str<<endl;
cout<<boost::format("%d:%s") % number % str<<endl;
cout<<boost::format("%|05d|:%|s|") % number % str<<endl;
cout<<boost::format("%1%:%|8t|%2%") % number % str<<endl;
}

//==============================================================================
#include <boost/algorithm/string.hpp>
void test_string_algo()
{
string str = " this is a test!";
cout<<boost::to_upper_copy(str)<<endl;
cout<<boost::trim_left_copy(str)<<endl;
string num = "0853";
boost::trim_left_if(num,boost::is_any_of("0"));
cout<<num<<endl;
if(boost::find_first(str, "is"))
{
cout<<"'is' in "<<str<<endl;
}
boost::replace_first(str, "this", "that");
cout<<str<<endl;
boost::erase_all(str, " ");
cout<<str<<endl;
}

//==============================================================================
#include <boost/tokenizer.hpp>
void test_tokenizer()
{
string str = "this is a test!";
boost::tokenizer<> tok(str);
for(boost::tokenizer<>::iterator it=tok.begin();it!=tok.end();++it)
{
cout<<*it<<endl;
}
boost::char_separator<char> sep(" ");
boost::tokenizer<boost::char_separator<char> > tok2(str, sep);
for(boost::tokenizer<boost::char_separator<char> >::iterator it=tok2.begin();it!=tok2.end();++it)
{
cout<<*it<<endl;
}
}

//==============================================================================
#include <boost/array.hpp>
void test_array()
{
boost::array<int,10> ai = {11,12,13};
for(boost::array<int,10>::iterator it=ai.begin();it!=ai.end();++it)
{
cout<<*it<<endl;
}
}

//==============================================================================
#include <boost/dynamic_bitset.hpp>
void test_dynamic_bitset()
{
cout<<endl<<">>>test_dynamic_bitset()"<<endl;
boost::dynamic_bitset<> db1(string("01000010"));
boost::dynamic_bitset<> db2(string("01000001"));
cout<<(db1 | db2)<<endl;
}

//==============================================================================
#include <boost/unordered_set.hpp>
#include <boost/unordered_map.hpp>
void test_unordered()
{
boost::unordered_set<int> us;
us.insert(1);
us.insert(2);
us.insert(5);
us.insert(25);
for(boost::unordered_set<int>::iterator it=us.begin();it!=us.end();++it)
{
cout<<*it<<endl;
}

boost::unordered_map<int,string> um;
um.insert(make_pair(1,"first"));
um.insert(make_pair(2,"second"));
um.insert(make_pair(5,"just"));
um.insert(make_pair(25,"soso"));
for(boost::unordered_map<int,string>::iterator it=um.begin();it!=um.end();++it)
{
cout<<it->first<<"-"<<it->second<<endl;
}
for(int i=0; i<um.bucket_count(); ++i)
{
cout<<"bucket["<<i<<"].size="<<um.bucket_size(i)<<endl;
}
}

//==============================================================================
#include <boost/bimap.hpp>
void test_bimap()
{
boost::bimap<int,string> bm;
bm.left.insert(make_pair(1,"first"));
bm.left.insert(make_pair(2,"second"));
bm.right.insert(make_pair("just",5));
bm.right.insert(make_pair("soso",25));
for(boost::bimap<int,string>::iterator it=bm.begin();it!=bm.end();++it)
{
cout<<"<"<<it->left<<","<<it->right<<">"<<endl;
}
for(boost::bimap<int,string>::left_iterator it=bm.left.begin();it!=bm.left.end();++it)
{
cout<<"left["<<it->first<<"]="<<it->second<<endl;
}
for(boost::bimap<int,string>::right_iterator it=bm.right.begin();it!=bm.right.end();++it)
{
cout<<"right["<<it->first<<"]="<<it->second<<endl;
}
}

//==============================================================================
#include <boost/assign.hpp>
void test_assign()
{
typedef boost::bimap<int,string> bm_t;
bm_t bm = boost::assign::list_of<bm_t::relation>(1,"just")(2,"soso");
for(bm_t::iterator it=bm.begin();it!=bm.end();++it)
{
cout<<"<"<<it->left<<","<<it->right<<">"<<endl;
}
}

//==============================================================================
#include <boost/circular_buffer.hpp>
void test_circular_buffer()
{
typedef boost::circular_buffer<int> cb_t;
cb_t cb = (boost::assign::list_of(1),2,3,4,5);
cb.push_back(6);
boost::assign::push_back(cb)(7),8,9;
for(cb_t::iterator it=cb.begin();it!=cb.end();++it)
{
cout<<*it<<endl;
}
}

//==============================================================================
#include <boost/tuple/tuple.hpp>
void test_tuple()
{
typedef boost::tuple<int,string> tp_t;
tp_t tp = boost::make_tuple(1,"just");
cout<<tp.get<0>()<<","<<tp.get<1>()<<endl;
}

//==============================================================================
#include <boost/any.hpp>
void test_any()
{
boost::any a(string("any_string"));
string str = boost::any_cast<string>(a);
cout<<str<<endl;

boost::array<boost::any,2> ar = {1,string("just")};
cout<<boost::any_cast<int>(ar[0])<<endl;
cout<<boost::any_cast<string>(ar[1])<<endl;
}

//==============================================================================
#include <boost/variant.hpp>
void test_variant()
{
typedef boost::variant<int,string> var_t;
var_t v1(123);
var_t v2(string("just"));
if(v2.type() == typeid(string))
{
cout<<"v2.type=string"<<endl;
}
}

//==============================================================================
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/json_parser.hpp>
void test_property_tree()
{
boost::property_tree::ptree pt;
pt.put("conf.user", "just");
pt.put("conf.pass", "soso");
boost::property_tree::write_xml("conf.xml", pt);
boost::property_tree::write_ini("conf.ini", pt);
boost::property_tree::write_json("conf.json", pt);

boost::property_tree::read_xml("conf.xml", pt);
cout<<pt.get<string>("conf.user")<<endl;
cout<<pt.get<string>("conf.pass")<<endl;

boost::property_tree::ptree child = pt.get_child("conf");
for(boost::property_tree::ptree::iterator it = child.begin();
it != child.end(); ++it)
{
cout<<it->first<<"->"<<it->second.get_value<string>()<<endl;
}

try
{
boost::property_tree::read_xml("ex_conf.xml", pt);
cout<<"comment: "<<pt.get<string>("conf.<xmlcomment>")<<endl;
cout<<"attr: "<<pt.get<string>("conf.username.<xmlattr>.id")<<endl;
}catch(std::exception& e)
{
cout<<e.what()<<endl;
}
}

//==============================================================================
#include <boost/timer.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
void test_date_time()
{
boost::timer t;
cout<<"tim elapsed: "<<t.elapsed()<<endl;

typedef boost::gregorian::date date_t;
date_t d1(2012,2,16);
date_t d2 = boost::gregorian::from_string("2012-02-17");
date_t d3 = boost::gregorian::from_undelimited_string("20120218");
date_t d4 = d3 + boost::gregorian::days(1);
cout<<boost::gregorian::to_iso_string(d1)<<endl;
cout<<boost::gregorian::to_iso_extended_string(d2)<<endl;
cout<<"dayofweek: "<<d3.day_of_week()<<endl;

boost::gregorian::date_period dp(d1, d4);
cout<<dp.end()<<endl;

typedef boost::posix_time::ptime ptime_t;
ptime_t p1 = boost::posix_time::time_from_string("2012-02-17 12:30:00");
cout<<boost::posix_time::to_iso_extended_string(p1)<<endl;

boost::posix_time::time_period tp(p1, boost::posix_time::hours(8));
cout<<tp.end()<<endl;
}

//==============================================================================
#include <boost/filesystem.hpp>
void test_filesystem()
{
typedef boost::filesystem::path path_t;
path_t p("/home/lhw/code/BoostTest/main.cpp");
cout<<"string: "<<p.string()<<endl;
cout<<"filename: "<<p.filename()<<endl;
cout<<"extension: "<<p.extension()<<endl;
cout<<"file_size: "<<boost::filesystem::file_size(p)<<endl;
cout<<"is_dir: "<<boost::filesystem::is_directory(p)<<endl;
cout<<"exists: "<<boost::filesystem::exists(p)<<endl;

boost::filesystem::create_directory("./tmp");
boost::filesystem::copy_file(p, "./tmp/tmp.txt");
boost::filesystem::remove_all("./tmp");

typedef boost::filesystem::recursive_directory_iterator rdit_t;
rdit_t end;
for(rdit_t it("/home/lhw/code"); it!=end; ++it)
{
if(it.level()==1 && boost::filesystem::is_directory(*it))
{
it.no_push();
}
cout<<"level["<<it.level()<<"]"<<*it<<endl;
}
}

//==============================================================================
#include <boost/smart_ptr.hpp>
void test_smart_ptr()
{
typedef boost::shared_ptr<string> str_ptr;
str_ptr sp1(new string("just soso"));
cout<<*sp1<<",use_count: "<<sp1.use_count()<<endl;

typedef vector<boost::shared_ptr<string> > vec_str;
vec_str v;
str_ptr s1(new string("just"));
str_ptr s2(new string("soso"));
v.push_back(s1);
v.push_back(s2);
}

//==============================================================================
#include <boost/pool/pool.hpp>
#include <boost/pool/object_pool.hpp>
#include <boost/pool/singleton_pool.hpp>
struct pool_tag
{
int m_id;
pool_tag(int id)
{
m_id = id;
}
~pool_tag()
{
}
};
void test_pool()
{
typedef boost::singleton_pool<pool_tag, sizeof(int)> spool;
int *p1 = (int*)spool::malloc();
cout<<"is_form: "<<spool::is_from(p1)<<endl;
spool::free(p1);

boost::object_pool<pool_tag> opool;
pool_tag* p2 = opool.construct(25);
cout<<"m_id="<<p2->m_id<<endl;
opool.destroy(p2);
}

//==============================================================================
#include <boost/assign.hpp>
#include <boost/utility/result_of.hpp>
#include <boost/ref.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
int sum(int a, int b)
{
return a+b;
};
struct square
{
void operator()(int& x)
{
x = x * x;
cout<<x<<",";
}

int sum(int a, int b)
{
return a+b;
}

void print(int& x, int i)
{
cout<<x+i<<",";
}
};
void test_bind()
{
typedef int (*Func)(int,int);
Func func = sum;
boost::result_of<Func(int,int)>::type x = sum(2,3)+func(2,3);
cout<<"typename: "<<typeid(x).name()<<"("<<x<<")"<<endl;

vector<int> v = (boost::assign::list_of(1),2,3,4,5);
for_each(v.begin(), v.end(), square());
cout<<endl;

string s("just so so.");
cout<<boost::ref(s).get()<<endl;
cout<<boost::unwrap_ref(s)<<endl;

cout<<"bind: "<<boost::bind(sum,2,3)()<<endl;
cout<<"bind: "<<boost::bind(sum,_1,_2)(2,3)<<endl;
cout<<"bind: "<<boost::bind<int>(sum,2,3)()<<endl;

square sq;
cout<<"bind: "<<boost::bind(&square::sum, &sq, 2, 3)()<<endl;

for_each(v.begin(), v.end(),
boost::bind(&square::print, &sq, _1, 1));
cout<<endl;

pair<int,string> p(123, "just");
cout<<boost::bind(&pair<int,string>::second, p)()<<endl;

boost::function<int(int,int)> f = sum;
if(f)
{
cout<<"bind func: "<<f(2,3)<<endl;
f = 0;
}

f = boost::bind(&square::sum, boost::ref(sq), _1, _2);
cout<<"bind class func: "<<f(2,3)<<endl;
}

//==============================================================================
#include <boost/regex.hpp>
void test_regex()
{
boost::smatch what;
string str1 = "just192.168.7.250soso";
boost::regex exp1("just.*soso");
boost::regex exp2("(\\d+\\.)(\\d+\\.)(\\d+\\.)(\\d+)");
boost::regex exp3("\\d+");
if(boost::regex_match(str1, exp1))
{
cout<<"match."<<endl;
}

if(boost::regex_search(str1, what, exp2))
{
cout<<"0: "<<what[0]<<endl;
cout<<"1: "<<what[1]<<endl;
cout<<"2: "<<what[2]<<endl;
cout<<"3: "<<what[3]<<endl;
cout<<"4: "<<what[4]<<endl;
}

string::const_iterator start = str1.begin();
string::const_iterator end = str1.end();
while(boost::regex_search(start, end, what, exp3))
{
start = what[0].second;
string msg(what[0].first, what[0].second);
cout<<msg<<endl;
}

string tmp = boost::regex_replace(str1, exp3, "*");
cout<<"after replace: "<<tmp<<endl;
}

//==============================================================================
#include <boost/thread.hpp>
queue<int> g_qu;
boost::mutex g_mu;
boost::condition_variable_any cond_g;
boost::condition_variable_any cond_p;
void myThread(boost::mutex& mu, int n)
{
try
{
boost::mutex::scoped_lock lock(mu);
for(int i=1; i<4; ++i)
{
cout<<"<"<<n<<">thread running "<<i<<endl;
boost::this_thread::sleep(boost::posix_time::seconds(1));
}
}catch(boost::thread_interrupted& ex)
{
cout<<"<"<<n<<">interrupted."<<endl;
}
}
bool isNotFull()
{
return (g_qu.size()<3);
}
bool isNotEmpty()
{
return (!g_qu.empty());
}
void GetThread(int id)
{
for(int i=0; i<5; ++i)
{
{
boost::mutex::scoped_lock lock(g_mu);
//while(g_qu.empty())
//{
// cond_g.wait(g_mu);
//}
cond_g.wait(g_mu, isNotEmpty);
cout<<"<"<<id<<">get: "<<g_qu.front()<<endl;
g_qu.pop();
}
cond_p.notify_one();
}
}
void PutThread(int id)
{
for(int i=0; i<10; ++i)
{
{
boost::mutex::scoped_lock lock(g_mu);
while(g_qu.size() >= 3)
{
cond_p.wait(g_mu);
}
cout<<"<"<<id<<">put: "<<i<<endl;
g_qu.push(i);
}
cond_g.notify_one();
boost::this_thread::sleep(boost::posix_time::seconds(1));
}
}
int fab(int n)
{
if(n==0 || n==1)
return 1;
return fab(n-1)+fab(n-2);
}
void test_thread()
{
boost::mutex mu;
boost::thread t1(myThread, boost::ref(mu), 1);
boost::thread t2(boost::bind(myThread, boost::ref(mu), 2));
boost::function<void(int)> f
= boost::bind(myThread, boost::ref(mu), _1);
boost::thread t3(f, 3);
t1.join();
t2.interrupt();
t2.join();
t3.join();

boost::thread g1(GetThread, 1);
boost::thread g2(GetThread, 2);
boost::thread p1(PutThread,1);
g1.join();
g2.join();
p1.join();

boost::packaged_task<int> pt(boost::bind(fab, 10));
boost::unique_future<int> uf = pt.get_future();
boost::thread(boost::move(pt));
uf.wait();
cout<<uf.get();
}

//==============================================================================
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
void test_asio()
{
boost::asio::io_service ios;
cout<<"now: "<<boost::posix_time::second_clock::local_time()<<endl;
boost::asio::deadline_timer t(ios, boost::posix_time::seconds(3));
cout<<"expires at: "<<t.expires_at()<<endl;
t.wait();
cout<<boost::posix_time::second_clock::local_time()
<<": time expires."<<endl;

boost::asio::ip::address addr =
boost::asio::ip::address::from_string("127.0.0.1");
//boost::asio::ip::tcp::endpoint ep(addr, 6688);
boost::asio::ip::tcp::endpoint ep(
boost::asio::ip::tcp::v4(), 6688);
boost::asio::ip::tcp::acceptor acc(ios, ep);
cout<<"["<<acc.local_endpoint().address()<<"]:"<<endl;
while(true)
{
boost::asio::ip::tcp::socket soc(ios);
acc.accept(soc);
cout<<soc.remote_endpoint().address()<<" connected."<<endl;
soc.write_some(boost::asio::buffer("hello, asio!"));
}
}

boost 使用列子的更多相关文章

  1. 如何用boost::serialization去序列化派生模板类(续)

    在 如何用boost::serialization去序列化派生模板类这篇文章中,介绍了序列化派生类模板类, 在写測试用例时一直出现编译错误,调了非常久也没跳出来,今天偶然试了一下...竟然调了出来. ...

  2. boost::mpl::eval_if的使用方法

    近期看boost的时候总是遇见这个eval_if,不知道啥意思,就没法看下去了,比方 前篇文章boost::serialization 拆分serialize函数分析时就出现这样一段代码: templ ...

  3. boost强分类器的实现

    boost.cpp文件下: bool CvCascadeBoost::train( const CvFeatureEvaluator* _featureEvaluator, int _numSampl ...

  4. Boost信号/槽signals2

    信号槽是Qt框架中一个重要的部分,主要用来解耦一组互相协作的类,使用起来非常方便.项目中有同事引入了第三方的信号槽机制,其实Boost本身就有信号/槽,而且Boost的模块相对来说更稳定. signa ...

  5. 玩转Windows服务系列——使用Boost.Application快速构建Windows服务

    玩转Windows服务系列——创建Windows服务一文中,介绍了如何快速使用VS构建一个Windows服务.Debug.Release版本的注册和卸载,及其原理和服务运行.停止流程浅析分别介绍了Wi ...

  6. Task三个列子的分享

    这次要分享的是C#Task任务的几个列子,感觉最实用的是封装的分页任务执行方法,这个方法步奏也是目前在我工作中执行多任务常用的,不知道各位也有这用的情况,那么开始吧. 1.顺序任务执行 //顺序任务执 ...

  7. boost::function的用法

    本片文章主要介绍boost::function的用法. boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象. 1.  介绍 Boost.Func ...

  8. Boost条件变量condition_variable_any

    Boost条件变量可以用来实现线程同步,它必须与互斥量配合使用.使用条件变量实现生产者消费者的简单例子如下,需要注意的是cond_put.wait(lock)是在等待条件满足.如果条件不满足,则释放锁 ...

  9. 新手,Visual Studio 2015 配置Boost库,如何编译和选择,遇到无法打开文件“libboost_thread-vc140-mt-gd-1_63.lib“的解决办法

    1,到官网下载最新的boost,www.boost.org 这里我下载的1-63版本. 2,安装,解压后运行bootstrap.bat文件.稍等一小会就OK. 3,编译boost库.注意一定要使用VS ...

随机推荐

  1. HttpURLConnection的使用

    import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; ...

  2. Unix系统编程()文件描述符和打开文件之间的关系

    目前学习到的是一个文件描述符对应着一个打开的文件,似乎是对应的关系.但是实际上并不是这样的.多个文件描述符指向同一个打开的文件,是可能的也是必要的.这些文件描述符可以在相同或者不同的进程中打开. 要理 ...

  3. RabbitMQ之HelloWorld【译】

    简介 RabbitMQ是一个消息代理,主要的想法很简单:它接收并转发消息.你可以把它当做一个邮局,当你发送邮件到邮筒,你相信邮差先生最终会将邮件投递给收件人.RabbitMQ在这个比喻里,是一个邮筒, ...

  4. 轻量级分布式 RPC 框架(转)

    RPC,即 Remote Procedure Call(远程过程调用),说得通俗一点就是:调用远程计算机上的服务,就像调用本地服务一样. RPC 可基于 HTTP 或 TCP 协议,Web Servi ...

  5. TensorFlow基础笔记(5) VGGnet_test

    参考 http://blog.csdn.net/jsond/article/details/72667829 资源: 1.相关的vgg模型下载网址 http://www.vlfeat.org/matc ...

  6. C++继承具体解释之二——派生类成员函数具体解释(函数隐藏、构造函数与兼容覆盖规则)

    在这一篇文章開始之前.我先解决一个问题. 在上一篇C++继承详解之中的一个--初探继承中,我提到了在派生类中能够定义一个与基类成员函数同名的函数,这样派生类中的函数就会覆盖掉基类的成员函数. 在谭浩强 ...

  7. 用原生Canvas写贪吃蛇及问题解决

    为了学习Canvas,写了这个小游戏贪吃蛇供自己和大家学习 Github: https://github.com/zhiyishou/Gsnake Play On: http://zhiyishou. ...

  8. 将UIView转成UIImage,将UIImage转成PNG/JPG

    分类: UIImageView2013-03-12 17:37 350人阅读 评论(0) 收藏 举报 //UIView -> UIImage #import “QuartzCore/Quartz ...

  9. git与sourceTree

    Window:http://my.oschina.net/lunqi/blog/500881?fromerr=bzaPk1Lx MAC:http://www.ithao123.cn/content-8 ...

  10. 引入外部文件的时候为什么省略http:

    [摘要]相信很多时候大家在使用网上的实例代码的时候,需要引入外部文件,然而它们通常都是省略了http:,这是为什么呢? 先给大家看一个简单的小例子,引入外部jquery文件: <script s ...