锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
浠ュ悗浣跨敤寰楃潃鐨?br>
#include <vector>
#include <algorithm>
#include <boost/assign.hpp>
#include <boost/function.hpp>
using namespace std;
using namespace boost;
using namespace boost::assign;
inline void print_(int t){cout<<t<<" ";}
inline void print(vector<int>& vec)
{
for_each(vec.begin(),vec.end(),print_);
cout<<endl;
}
//! 鍏ㄦ帓鍒楁祴璇?/span>
void test1()
{
vector<int> vec;
vec += 1,2,3,4,5,6,7,8;
sort(vec.begin(),vec.end());
int i = 0;
do
{
print(vec);
i++;
}
while(next_permutation(vec.begin(),vec.end()));
std::cout<<i<<std::endl;
}
//! 緇勫悎嫻嬭瘯
size_t test2(int n,int m,boost::function<void(std::vector<int>& vec)> fn)
{
vector<int> p,set;
p.insert(p.end(),m,1);
p.insert(p.end(),n-m,0);
for(int i = 0;i != p.size();++i)
set.push_back(i+1);
vector<int> vec;
size_t cnt = 0;
do{
for(int i = 0;i != p.size();++i)
if(p[i])
vec.push_back(set[i]);
fn(vec);
cnt ++;
vec.clear();
}while(prev_permutation( p.begin(), p.end()));
return cnt;
}
int main()
{
test1();
std::cout<<test2(20,3,print)<<std::endl;
return 0;
}
]]>
鍦ㄤ嬌鐢ㄧ殑鏃跺欓渶瑕佽緇嗘敞鎰?br>
boost::tokenizer<> tok(std::string("鐚?nbsp;鐙?nbsp;鐚?/span>"));
for(boost::tokenizer<>::iterator beg=tok.begin();beg!=tok.end();++beg)
{
tag += *beg;
tag += "+";
}
澶у璇磋緇撴灉鍚?br>
]]>
涓涓棶棰樺氨鏄紩鎿庝腑鐨凴efPtr璁捐鏈夌偣闂
绱㈡т笅涓紩鎿庣増鏈氨鏄敤boost搴撳惂
鍏嶅緱涓浜涘熀紜闇瑕佽嚜宸卞啓
瑕佷嬌鐢╞oost搴撳叾涓殑鏅鴻兘鎸囬拡蹇呬笉鍙皯
鏋楁灄鎬繪繪湁濂藉嚑涓被鍨?br>涓嶈繃甯哥敤寰楄繕鏄?涓?br>scoped_ptr,
shared_ptr,
weak_ptr
榪欓噷鍏堣shared_ptr
瀹冭繕鏄紩鐢ㄨ鏁扮被鍨嬬殑鎸囬拡鍜?br>璨屼技姣旇緝濂界敤(浣嗘槸鏍規湰榪樻槸鍦ㄤ簬浣跨敤鐨勪漢)
涓轟簡姝g‘浣跨敤瀹冮渶瑕佹敞鎰忓嚑涓棶棰?br>1.澶氱嚎紼嬬幆澧冨拰寰幆寮曠敤
搴旇閰嶅悎weak_ptr
2.浣跨敤浜唖hared_ptr灝變笉搴旇浣跨敤鍏朵粬鍐呭瓨綆$悊鏈哄埗
3.涓嶈鏋勯犱復鏃剁殑shared_tr浣滀負鍑芥暟鍙傛暟
4.鍏朵粬...
]]>
#include <iostream>
#include <boost/pool/pool.hpp>
#include <boost/pool/object_pool.hpp>
using namespace std;
using namespace boost;
class Object
{
public:
Object(){cout<<"new obj"<<endl;}
~Object(){cout<<"delete obj"<<endl;}
};
int main(int argc, char *argv[])
{
//!姣忔鍒嗛厤鐨勫潡鐨勫ぇ灝?/span>
const int blocksize = sizeof(int);
boost::pool<> alloc(blocksize);
for(int i = 0; i < 100; i++)
{
//! 鍒嗛厤
int* ptr = (int*)alloc.malloc();
cout<<*ptr<<endl;
//! 閲婃斁
alloc.free(ptr);
}
//!object pool姣忔闇瑕佹寚瀹氬璞″ぇ灝?nbsp;
boost::object_pool<Object> pool_alloc;
for(int i = 0;i < 100;i++)
{
//! 鍒嗛厤鍐呭瓨
void* mem = pool_alloc.malloc();
//! 鏋勯?/span>
Object *obj = new(mem)Object();
//! 鏋愭瀯
pool_alloc.destroy(obj);
}
system("PAUSE");
return EXIT_SUCCESS;
}
]]>
#include <boost/function.hpp>
鍩烘湰鐨刦unction瀵硅薄渚嬪瓙
boost::function<int(const char*,&int)> f;
浠g爜璇ュ嚱瀛愬搴旂殑鍑芥暟鍏惰繑鍥炲間負int綾誨瀷,濂規湁涓?涓弬鏁板垎鍒負const char*鍜?amp;int綾誨瀷
涓涓畝鍗曠殑渚嬪瓙濡備笅鎵紺?
#include <boost/function.hpp>
inline int Sum(const int a,const int b)
{
return a + b;
}
int main()
{
boost::function<int(const int,const int)> sum_ptr;
sum_ptr = ∑
std::cout<<"1+2=:?"<<sum_ptr(1,2);
system("PAUSE");
return EXIT_SUCCESS;
}
#include <boost/function.hpp>
#include <functional>
struct Adder
{
Adder(int val):value(val){}
int Add(int x){return x*value;}
int value;
};
int main()
{
//! 瀵瑰簲鍑芥暟榪斿洖鍊糹nt鍙傛暟涓篿nt
boost::function<int(int)>f;
Adder add(7);
//! 緇戝畾鎴愬憳鍑芥暟鍒癰oost::function<
>
f = std::bind1st(std::mem_fun(&Adder::Add),&add);
std::cout<<f(5)<<std::endl;
system("PAUSE");
return EXIT_SUCCESS;
}
#include <boost/function.hpp>
#include <functional>
struct Div
{
float operator()(int x, int y)const
{
return((float)x)/y;
}
};
int main()
{
//! 2
boost::function<float(int,int)> div;
div = Div();
std::cout<<div(1,2)<<std::endl;
system("PAUSE");
return EXIT_SUCCESS;
}
]]>
#include <iostream>
#include <boost/assign.hpp>
#include <boost/array.hpp>
#include <algorithm>
#include <iterator>
using namespace std;
using namespace boost;
using namespace boost::assign;
int main(int argc, char *argv[])
{
//! vector璧嬪?/span>
vector<int> v;
v += 1,2,3,4,5,6,7,8,9;
copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\n"));
//! map
map<string,int> m;
insert(m)("foo",1)("bar",2)("ss",3);
std::cout<<m.size()<<std::endl;
//! boost array.
typedef array<float,6> Array;
#if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
Array a = list_of(1.1)(2.2)(3.3)(4.4)(5.5)(6.6).to_array(a);
#else
Array a = list_of(1.1)(2.2)(3.3)(4.4)(5.5)(6.6);
#endif
copy(a.begin(),a.end(),ostream_iterator<float>(cout,"\n"));
typedef boost::tuple<int,std::string,int> tuple;
std::vector<tuple> vt = tuple_list_of(1,"foo",2)(3,"bar",4);
std::map<std::string,int> mp = pair_list_of("foo",3)("bar",5);
system("PAUSE");
return EXIT_SUCCESS;
}
//! ccsdu2004
]]>
鍩烘湰鐨勭爺鍙戝涓?
#include <iostream>
#include <boost/filesystem.hpp>
using namespace std;
namespace fs = boost::filesystem;
int main(int argc, char *argv[])
{
std::cout<<"sizeof(intmax_t)is:"<<sizeof(boost::intmax_t)<<'\n';
fs::path path("main.cpp",fs::native);
if(!fs::exists(path))
{
return -1;
}
if(!fs::is_regular(path))
return -1;
std::cout<<"size is:"<<fs::file_size(path)<<"瀛楄妭"<<std::endl;
fs::path full_path(fs::initial_path<fs::path>());
full_path = fs::system_complete(fs::path("main.cpp"));
//! 鑾峰彇緇欏畾鏂囦歡鍏ㄨ礬寰?nbsp;
cout<<"full path name is:"<<full_path.file_string()<<std::endl;
//! 媯嫻嬫槸涓嶆槸璺緞
cout<<"is path:"<<fs::is_directory(full_path)<<std::endl;
unsigned long file_count = 0;
unsigned long dir_count = 0;
unsigned long other_count = 0;
unsigned long err_count = 0;
full_path = fs::system_complete(fs::path("C:\\WINDOWS"));
//! 鐩綍榪唬
fs::directory_iterator end_iter;
for(fs::directory_iterator dir_itr(full_path);dir_itr != end_iter;++dir_itr)
{
try
{
if(fs::is_directory(dir_itr->status()))
{
++dir_count;
std::cout<<dir_itr->path().filename()<<"[directory]\n";
}
else if(fs::is_regular_file(dir_itr->status()))
{
++file_count;
std::cout<<dir_itr->path().filename()<<"\n";
//! 鍏ㄨ礬寰勫悕瀛?nbsp;
std::cout<<dir_itr->path().native_file_string()<<std::endl;
}
else
{
++other_count;
std::cout << dir_itr->path().filename() <<"[other]\n";
}
}
catch(const std::exception & ex)
{
++err_count;
std::cout<<dir_itr->path().filename()<<" "<<ex.what()<<std::endl;
}
}
std::cout<<"\n"<<file_count<<"files\n"<<dir_count<<" directories\n"<<other_count<<" others\n"<<err_count<<" errors\n";
//! 鐢熸垚鏂囦歡鐩綍
fs::create_directories(fs::path("filesystem"));
system("PAUSE");
return EXIT_SUCCESS;
}
//! ccsdu2004
]]>
瑁佸壀
澶у皬鍐欐浛鎹?br>姝e垯琛ㄨ揪寮?br>鍒囧壊
鍒ゆ柇
鍜屾摝闄ゆ搷浣滅瓑絳?br>
#include <string>
#include <vector>
#include <iostream>
#include <iterator>
#include <functional>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
int main()
{
//! 瀛楃涓?nbsp;
string str(" abc-*-ABC-*-aBc ");
//! 浠?鎴栬?鍒囧壊瀛楃涓?nbsp;
vector<std::string> ret;
split(ret,str,is_any_of("-*"),token_compress_on);
for(unsigned int index=0;index<ret.size();index++)
{
cout<<index<<":"<<ret[index]<<endl;
};
//! 鍒囨崲涓哄皬鍐?/span>
to_lower(str);
cout<<"*"<<str<<"*"<<endl;
//! 鍘繪帀宸﹁竟鐨勭┖鏍?/span>
trim_left(str);
cout<<"*"<<str<<"*"<<endl;
//! 鍘繪帀鍙寵竟鐨勭┖鏍?/span>
trim_right(str);
cout<<"*"<<str<<"*"<<endl;
//! 鏇挎崲
replace_all(str,"a","A");
cout<<str<<endl;
//! 鎿﹂櫎
cout<<erase_all_copy(str,"A")<<endl;
replace_nth(str,"c",2,"ccsdu2004");
cout<<str<<endl;
//! 鎿﹂櫎緇欏畾鑼冨洿
cout<<erase_range_copy(str,make_iterator_range(str.begin()+2,str.begin()+5))<<endl;
cout<<"is start with:A:"<<starts_with(str,string("A"))<<endl;
cout<<"is end with:C:"<<ends_with(str,string("C"))<<endl;
cout<<"is contain with:ccs:"<<contains(str,string("ccs"))<<endl;
cout<<endl;
system("PAUSE");
return 0;
}
]]>
#include <list>
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <boost/algorithm/minmax.hpp>
#include <boost/algorithm/minmax_element.hpp>
using namespace std;
inline void Print(int i)
{
std::cout<<i<<std::endl;
}
inline int Rand()
{
return rand()%10;
}
int main()
{
list<int> l;
typedef list<int>::const_iterator iterator;
//! 浣跨敤緇欏畾娉涘嚱瀛愮敓鎴?2涓殢鏈烘暟騫舵帹鍏ラ摼琛?nbsp;
generate_n(front_inserter(l),12,Rand);
std::for_each(l.begin(),l.end(),Print);
std::cout<<"list size is:"<<l.size()<<std::endl;
//! 鑾峰彇緇欏畾搴忓垪鐨勫澶ф渶灝忓?榪斿洖涓簊td::pair<..,..>
pair<iterator,iterator> result = boost::minmax_element(l.begin(),l.end());
cout<<"the smallest element is:"<<*(result.first)<<endl;
cout<<"the largest element is:"<<*(result.second)<<endl;
//! 鑾峰彇絎竴涓渶灝忓厓绱?nbsp;
iterator minitr = boost::first_min_element(l.begin(),l.end());
cout<<"first min element is:"<<*minitr<<endl;
system("PAUSE");
return 1;
}
//!ccsdu2004
]]>