第5章
lexical_cast介绍
测试代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream>
#include < string>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace boost;
void test1(){
int x=lexical_cast< int>( " 100 ");
long y=lexical_cast< long>( " 2000 ");
float pai=lexical_cast< float>( " 3.14159e5 ");
double e=lexical_cast< double>( " 2.71828 ");
cout<<x<< " "<<y<< " "<<pai<< " "<<e<<endl;
string str=lexical_cast< string>( 456);
cout<<str<<endl;
cout<<lexical_cast< string>( 0.6810)<<endl;
cout<<lexical_cast< string>( 0x10)<<endl;
}
int main(){
test1();
}
#include < string>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace boost;
void test1(){
int x=lexical_cast< int>( " 100 ");
long y=lexical_cast< long>( " 2000 ");
float pai=lexical_cast< float>( " 3.14159e5 ");
double e=lexical_cast< double>( " 2.71828 ");
cout<<x<< " "<<y<< " "<<pai<< " "<<e<<endl;
string str=lexical_cast< string>( 456);
cout<<str<<endl;
cout<<lexical_cast< string>( 0.6810)<<endl;
cout<<lexical_cast< string>( 0x10)<<endl;
}
int main(){
test1();
}
boost中的format格式化输出库
format中为什么使用操作符重载
为什使用%
format格式化语法
测试代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
format的性能
boost中的字符串算法string_algo
string_algo概述,其中包括五大类算法:大小写转换,判断式与分类,修剪,查找与替换,分割与合并
大小写转换
判断式
判断式(函数对象)
字符串查找
替换与删除
字符串分割
字符串合并
测试代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream>
#include < string>
#include <vector>
#include <boost/algorithm/ string.hpp>
using namespace std;
using namespace boost;
void test1(){
string str( " readme.txt ");
if(ends_with(str, " txt ")){
cout<<to_upper_copy(str)+ " UPPER "<<endl;
}
replace_first(str, " readme ", " followme ");
cout<<str<<endl;
vector< char> v(str.begin(),str.end());
vector< char> v2=to_upper_copy(erase_first_copy(v, " txt "));
for( int i= 0;i<v2.size();i++)
cout<<v2[i];
cout<<endl;
}
void test2(){
string str( " I Don't Know.\n ");
cout<<to_upper_copy(str);
cout<<str;
to_lower(str);
cout<<str;
}
void test3(){
string str( " Power Bomb ");
cout<<iends_with(str, " bomb ")<<endl;
cout<<ends_with(str, " bomb ")<<endl;
cout<<starts_with(str, " Pow ")<<endl;
cout<<contains(str, " er ")<<endl;
}
int main(){
test2();
}
#include < string>
#include <vector>
#include <boost/algorithm/ string.hpp>
using namespace std;
using namespace boost;
void test1(){
string str( " readme.txt ");
if(ends_with(str, " txt ")){
cout<<to_upper_copy(str)+ " UPPER "<<endl;
}
replace_first(str, " readme ", " followme ");
cout<<str<<endl;
vector< char> v(str.begin(),str.end());
vector< char> v2=to_upper_copy(erase_first_copy(v, " txt "));
for( int i= 0;i<v2.size();i++)
cout<<v2[i];
cout<<endl;
}
void test2(){
string str( " I Don't Know.\n ");
cout<<to_upper_copy(str);
cout<<str;
to_lower(str);
cout<<str;
}
void test3(){
string str( " Power Bomb ");
cout<<iends_with(str, " bomb ")<<endl;
cout<<ends_with(str, " bomb ")<<endl;
cout<<starts_with(str, " Pow ")<<endl;
cout<<contains(str, " er ")<<endl;
}
int main(){
test2();
}
第6章
第7章
array类摘要
测试代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <boost/array.hpp>
using namespace std;
using namespace boost;
int main(){
const int N= 10;
array< int,N> ar;
for( int i= 0;i<N;i++)
ar[i]=rand()% 100;
copy(ar.begin(),ar.end(),ostream_iterator< int>(cout, " "));
cout<<endl;
}
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <boost/array.hpp>
using namespace std;
using namespace boost;
int main(){
const int N= 10;
array< int,N> ar;
for( int i= 0;i<N;i++)
ar[i]=rand()% 100;
copy(ar.begin(),ar.end(),ostream_iterator< int>(cout, " "));
cout<<endl;
}
dynamic_bitset
测试代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream>
#include <iterator>
#include <algorithm>
#include <boost/dynamic_bitset.hpp>
using namespace std;
using namespace boost;
int main(){
int n;
cin>>n;
dynamic_bitset<> db(n);
db. set();
for(dynamic_bitset<>::size_type i=db.find_next( 1);
i!=dynamic_bitset<>::npos;
i=db.find_next(i)){
for(dynamic_bitset<>::size_type j=db.find_next(i);
j!=dynamic_bitset<>::npos;
j=db.find_next(j)){
if(j%i== 0)
db[j]= 0;
}
}
for(dynamic_bitset<>::size_type i=db.find_next( 2);
i!=dynamic_bitset<>::npos;
i=db.find_next(i))
cout<<i<< " , ";
cout<<endl;
}
#include <iterator>
#include <algorithm>
#include <boost/dynamic_bitset.hpp>
using namespace std;
using namespace boost;
int main(){
int n;
cin>>n;
dynamic_bitset<> db(n);
db. set();
for(dynamic_bitset<>::size_type i=db.find_next( 1);
i!=dynamic_bitset<>::npos;
i=db.find_next(i)){
for(dynamic_bitset<>::size_type j=db.find_next(i);
j!=dynamic_bitset<>::npos;
j=db.find_next(j)){
if(j%i== 0)
db[j]= 0;
}
}
for(dynamic_bitset<>::size_type i=db.find_next( 2);
i!=dynamic_bitset<>::npos;
i=db.find_next(i))
cout<<i<< " , ";
cout<<endl;
}
unordered:boost中的散列容器
boost中unordered散列集合简介:
散列集合unordered_set用法
测试代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream>
#include <cstdlib>
#include <ext/hash_set>
#include <boost/assign.hpp>
#include <boost/unordered_set.hpp>
using namespace std;
using namespace boost;
using namespace __gnu_cxx;
template < class T>
void hash_func(){
using namespace boost::assign;
T s=(list_of( 1), 2, 3, 4, 5);
typename T::iterator p;
for(p=s.begin();p!=s.end();++p)
cout<<*p<< ' ';
cout<<endl;
cout<<s.size()<<endl;
s.clear();
cout<<s.empty()<<endl;
s.insert( 8);
s.insert( 45);
cout<<s.size()<<endl;
cout<<*s.find( 8)<<endl;
s.erase( 45);
for(p=s.begin();p!=s.end();++p)
cout<<*p<< ' ';
cout<<endl;
}
int main(){
hash_func<hash_set< int> >();
hash_func<unordered_set< int> >();
}
#include <cstdlib>
#include <ext/hash_set>
#include <boost/assign.hpp>
#include <boost/unordered_set.hpp>
using namespace std;
using namespace boost;
using namespace __gnu_cxx;
template < class T>
void hash_func(){
using namespace boost::assign;
T s=(list_of( 1), 2, 3, 4, 5);
typename T::iterator p;
for(p=s.begin();p!=s.end();++p)
cout<<*p<< ' ';
cout<<endl;
cout<<s.size()<<endl;
s.clear();
cout<<s.empty()<<endl;
s.insert( 8);
s.insert( 45);
cout<<s.size()<<endl;
cout<<*s.find( 8)<<endl;
s.erase( 45);
for(p=s.begin();p!=s.end();++p)
cout<<*p<< ' ';
cout<<endl;
}
int main(){
hash_func<hash_set< int> >();
hash_func<unordered_set< int> >();
}
散列映射简介:unordered_map
undered_map散列映射用法
测试代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream>
#include <cstdlib>
#include <ext/hash_map>
#include <boost/assign.hpp>
#include <boost/unordered_map.hpp>
using namespace std;
using namespace boost;
using namespace __gnu_cxx;
int main(){
using namespace boost::assign;
unordered_map< int, string> um=map_list_of( 1, " one ")( 2, " two ")( 3, " three ");
um.insert(make_pair( 10, " ten "));
cout<<um[ 10]<<endl;
um[ 11]= " eleven ";
um[ 15]= " fifteen ";
unordered_map< int, string>::iterator p;
for(p=um.begin();p!=um.end();++p)
cout<<p->first<< " - "<<p->second<< " , ";
cout<<endl;
um.erase( 11);
cout<<um.size()<<endl;
hash_map< int, string> hm=map_list_of( 4, " four ")( 5, " five ")( 6, " six ");
hash_map< int, string>::iterator q;
for(q=hm.begin();q!=hm.end();++q)
cout<<q->first<< " - "<<q->second<< " , ";
cout<<endl;
}
#include <cstdlib>
#include <ext/hash_map>
#include <boost/assign.hpp>
#include <boost/unordered_map.hpp>
using namespace std;
using namespace boost;
using namespace __gnu_cxx;
int main(){
using namespace boost::assign;
unordered_map< int, string> um=map_list_of( 1, " one ")( 2, " two ")( 3, " three ");
um.insert(make_pair( 10, " ten "));
cout<<um[ 10]<<endl;
um[ 11]= " eleven ";
um[ 15]= " fifteen ";
unordered_map< int, string>::iterator p;
for(p=um.begin();p!=um.end();++p)
cout<<p->first<< " - "<<p->second<< " , ";
cout<<endl;
um.erase( 11);
cout<<um.size()<<endl;
hash_map< int, string> hm=map_list_of( 4, " four ")( 5, " five ")( 6, " six ");
hash_map< int, string>::iterator q;
for(q=hm.begin();q!=hm.end();++q)
cout<<q->first<< " - "<<q->second<< " , ";
cout<<endl;
}
性能比较:
![](https://i-blog.csdnimg.cn/blog_migrate/06cfad6083408e6f0fe7a8b9594bab6c.png)
测试代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream>
#include <typeinfo>
#include < set>
#include <ext/hash_set>
#include <boost/unordered_set.hpp>
#include <boost/random.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
// #include <boost/date_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#define BOOST_DATE_TIME_SOURCE
#define BOOSt_DATE_TIME_POSIX_TIME_STD_CONFIG
using namespace std;
using namespace boost;
// using namespace boost::gregorian;
using namespace boost::posix_time;
using namespace __gnu_cxx;
template < class Clock=microsec_clock>
class basic_ptimer{
public:
basic_ptimer(){
restart();
}
void restart(){
_start_time=Clock::local_time();
}
void elapsed() const{
cout<<Clock::local_time()-_start_time;
}
~basic_ptimer(){
elapsed();
}
private:
ptime _start_time;
};
typedef basic_ptimer<microsec_clock> ptimer;
template < class T>
void fill_set(T &c){
variate_generator<mt19937,uniform_int<> >
gen(mt19937(),uniform_int<>( 0, 100));
for( int i= 0;i< 10000;++i)
c.insert(gen());
}
template < class T>
void test_perform(){
T c;
cout<<typeid(c).name()<< ' ';
{
ptimer t;
fill_set(c);
}
cout<< " ";
{
ptimer t;
c.count( 10);
}
cout<< " ";
{
ptimer t;
c.find( 20);
}
cout<<endl;
}
int main(){
test_perform<multiset< int> >();
test_perform<hash_multiset< int> >();
test_perform<unordered_multiset< int> >();
}
#include <typeinfo>
#include < set>
#include <ext/hash_set>
#include <boost/unordered_set.hpp>
#include <boost/random.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
// #include <boost/date_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#define BOOST_DATE_TIME_SOURCE
#define BOOSt_DATE_TIME_POSIX_TIME_STD_CONFIG
using namespace std;
using namespace boost;
// using namespace boost::gregorian;
using namespace boost::posix_time;
using namespace __gnu_cxx;
template < class Clock=microsec_clock>
class basic_ptimer{
public:
basic_ptimer(){
restart();
}
void restart(){
_start_time=Clock::local_time();
}
void elapsed() const{
cout<<Clock::local_time()-_start_time;
}
~basic_ptimer(){
elapsed();
}
private:
ptime _start_time;
};
typedef basic_ptimer<microsec_clock> ptimer;
template < class T>
void fill_set(T &c){
variate_generator<mt19937,uniform_int<> >
gen(mt19937(),uniform_int<>( 0, 100));
for( int i= 0;i< 10000;++i)
c.insert(gen());
}
template < class T>
void test_perform(){
T c;
cout<<typeid(c).name()<< ' ';
{
ptimer t;
fill_set(c);
}
cout<< " ";
{
ptimer t;
c.count( 10);
}
cout<< " ";
{
ptimer t;
c.find( 20);
}
cout<<endl;
}
int main(){
test_perform<multiset< int> >();
test_perform<hash_multiset< int> >();
test_perform<unordered_multiset< int> >();
}
boost中的树形结构:property_tree
读取配置信息
写入配置信息:
conf.xml文件内容及测试代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<
conf
>
< gui >1 </ gui >
< theme >matrix </ theme >
< urls >
< url >http://www.url1.com </ url >
< url >http://www.url2.com </ url >
< url >http://www.url3.com </ url >
</ urls >
< clock_style >24 </ clock_style >
</ conf >
< gui >1 </ gui >
< theme >matrix </ theme >
< urls >
< url >http://www.url1.com </ url >
< url >http://www.url2.com </ url >
< url >http://www.url3.com </ url >
</ urls >
< clock_style >24 </ clock_style >
</ conf >
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream>
#include < string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
using std::cout;
using std::endl;
using std:: string;
void parser(){
using namespace boost::property_tree;
ptree pt;
read_xml( " conf.xml ",pt);
cout<<pt. get< string>( " conf.theme ")<<endl;
cout<<pt. get< int>( " conf.clock_style ")<<endl;
cout<<pt. get< long>( " conf.gui ")<<endl;
cout<<pt. get( " conf.no_prop ", 100)<<endl;
ptree child;
ptree::iterator pos;
child=pt.get_child( " conf.urls ");
for(pos=child.begin(); pos!=child.end();++pos)
cout<<pos->second.data()<< " ";
cout<<endl;
cout<< " ==========writing=========== "<<endl;
pt.put( " conf.theme ", " Matrix Reloaded ");
pt.put( " conf.clock_style ", 12);
pt.put( " conf.gui ", 0);
pt.put( " conf.urls.url ", " http://www.url4.org ");
pt.put( " conf.urls.url ", " http://www.url5.org ");
write_xml(cout,pt);
}
int main(){
parser();
}
#include < string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
using std::cout;
using std::endl;
using std:: string;
void parser(){
using namespace boost::property_tree;
ptree pt;
read_xml( " conf.xml ",pt);
cout<<pt. get< string>( " conf.theme ")<<endl;
cout<<pt. get< int>( " conf.clock_style ")<<endl;
cout<<pt. get< long>( " conf.gui ")<<endl;
cout<<pt. get( " conf.no_prop ", 100)<<endl;
ptree child;
ptree::iterator pos;
child=pt.get_child( " conf.urls ");
for(pos=child.begin(); pos!=child.end();++pos)
cout<<pos->second.data()<< " ";
cout<<endl;
cout<< " ==========writing=========== "<<endl;
pt.put( " conf.theme ", " Matrix Reloaded ");
pt.put( " conf.clock_style ", 12);
pt.put( " conf.gui ", 0);
pt.put( " conf.urls.url ", " http://www.url4.org ");
pt.put( " conf.urls.url ", " http://www.url5.org ");
write_xml(cout,pt);
}
int main(){
parser();
}
第8章 算法
foreach使用
测试代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream>
#include <vector>
#include < string>
#include <boost/ foreach.hpp>
#include <boost/assign.hpp>
using namespace std;
void foreach_(){
using namespace boost::assign;
vector< int> v=(list_of( 1), 2, 3, 4, 5);
BOOST_FOREACH( int x,v){
cout<<x<< " ";
}
cout<<endl;
string str( " boost foreach ");
BOOST_FOREACH( char& c,str){
cout<<c<< " - ";
}
cout<<endl;
}
int main(){
foreach_();
}
#include <vector>
#include < string>
#include <boost/ foreach.hpp>
#include <boost/assign.hpp>
using namespace std;
void foreach_(){
using namespace boost::assign;
vector< int> v=(list_of( 1), 2, 3, 4, 5);
BOOST_FOREACH( int x,v){
cout<<x<< " ";
}
cout<<endl;
string str( " boost foreach ");
BOOST_FOREACH( char& c,str){
cout<<c<< " - ";
}
cout<<endl;
}
int main(){
foreach_();
}
第9章
伪随机数random
测试代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream>
#include <ctime>
#include <boost/random.hpp>
using namespace std;
using namespace boost;
void test1(){
mt19937 rng(time( 0));
for( int i= 0;i< 100;i++){
cout<<rng()<< " ";
}
cout<<endl;
}
int main(){
test1();
}
#include <ctime>
#include <boost/random.hpp>
using namespace std;
using namespace boost;
void test1(){
mt19937 rng(time( 0));
for( int i= 0;i< 100;i++){
cout<<rng()<< " ";
}
cout<<endl;
}
int main(){
test1();
}
第12章 多线程
thread库
使用thread库
时间功能
互斥量
互斥量用法
互斥量实例:
线程对象
创建线程,启动线程
join与timed_join
与线程执行体分离
使用bind和function
操作线程
中断线程
启动/禁用线程中断
线程组
条件变量
条件变量用法