先來(lái)看看如何賦值把:
#include <iostream>#include <vector>#include <string>#include<deque>#include <set>#include <map>#include <boost/assign.hpp>using namespace std; int _tmain(int argc, _TCHAR* argv[]){ using namespace boost::assign; //使用list_of vector<int> v=list_of(1)(2)(3)(4)(5); deque<string>d=(list_of("hello")("rollen")); set<int>s=(list_of(10),20,30,40); map<int, string>m=list_of(make_pair(1,"hello"))(make_pair(2,"rollen")); //list_of可以全部使用括號(hào),也可以將括號(hào)和逗號(hào)一起使用,但是對(duì)于后者需要 // 將整個(gè)lits_of用括號(hào)括起來(lái)。否則編譯器無(wú)法推導(dǎo)出list_of的類型而無(wú)法賦值。 // 下面使用map_list_of 和pair_list_of map<int,int>mp=map_list_of(1,1)(2,2)(3,3); map<int,string>mp2=pair_list_of(1,"hello")(2,"rollen"); //其實(shí)還有tuple_list_of}
#include <iostream>#include <vector>#include <string>#include<deque>#include <set>#include <map>#include <multiset>#include <boost/assign.hpp>using namespace std;//減少重復(fù)輸入 int _tmain(int argc, _TCHAR* argv[]){ using namespace boost::assign; vector<int>v=list_of(1).repeat(2,3)(4)(5); //將3重復(fù)2次 //v=1,3,3,4,5 multiset<int>ms; insert(ms).repeat_fun(5,&rand).repeat(2,1),10; //ms=x,x,x,x,x,1,1,10 deque<int>d; push_front(d).range(v.begin(),v.end()); //將一個(gè)序列的元素插入另外一個(gè)序列 //d=1,3,3,4,5}
與非標(biāo)準(zhǔn)容器一起使用
#include <iostream>#include <vector>#include <string>#include <stack>#include <queue>#include <boost/assign.hpp>using namespace std;//與非標(biāo)準(zhǔn)容器一起使用 int _tmain(int argc, _TCHAR* argv[]){ using namespace boost::assign; stack<int>s=(list_of(1),2,3).to_adapter(); while(!s.empty()){ cout<<s.top()<<" "; s.pop(); } cout<<endl; queue<string>q=(list_of("hello")("rollen").repeat(2,"holt")).to_adapter(); while(!q.empty()){ cout<<q.front()<<" "; q.pop(); } cout<<endl; priority_queue<double>pq=(list_of(1.21)(2.23)).to_adapter(); while(!pq.empty()){ cout<<pq.top()<<" "; pq.pop(); } cout<<endl;}
assign也支持部分不在STL中定義的非標(biāo)準(zhǔn)容器,比如slist和hash_map hash_set 用法和標(biāo)準(zhǔn)容器一樣、
此外,assign也支持大部分Boost的庫(kù)容器
#include <iostream>#include <vector>#include <string>#include <boost/assign.hpp>using namespace std;//list_of的嵌套使用// 構(gòu)建二維數(shù)組 int _tmain(int argc, _TCHAR* argv[]){ using namespace boost::assign; vector<vector<int> >v=list_of(list_of(1)(2))(list_of(3)(4)); v+=list_of(5)(6),list_of(7)(8); int a=1,b=2,c=3; vector<int>v1=cref_list_of<3>(a)(b)(c); //也可以使用ref_list_of assert(v.size()==3);}
#include <boost/swap.hpp>using namespace std;//交換兩個(gè)數(shù)組,兩個(gè)數(shù)組的長(zhǎng)度必須一致 int _tmain(int argc, _TCHAR* argv[]){ int a1[10]; int a2[10]; std::fill_n(a1,10,1); std::fill_n(a2,10,2); boost::swap(a1,a2);}
特化 swap
#include <iostream>#include <vector>#include <string>#include <boost/swap.hpp>using namespace std;class point{public: explicit point(int a,int b,int c):x(a),y(b),z(c){} void print()const{ cout<<x<<" "<<y<<" "<<z<<endl; } void swap(point &p){ std::swap(x,p.x); std::swap(y,p.y); std::swap(z,p.z); cout<<"inner swap"<<endl; }private: int x,y,z;};//特化std::swap 原則上不能動(dòng)stdnamespace std{ template<> void swap(point &x,point &y){ x.swap(y); }} int _tmain(int argc, _TCHAR* argv[]){ point a(1,2,3); point b(4,5,6); cout<<"std swap"<<endl; std::swap(a,b); cout<<"boost swap"<<endl; boost::swap(a,b);}
由于我們特化了swap,因此boost::swap 和std::swap效果一樣
特化ADL可找到的swap
#include <iostream>#include <vector>#include <string>#include <boost/swap.hpp>using namespace std;class point{public: explicit point(int a,int b,int c):x(a),y(b),z(c){} void print()const{ cout<<x<<" "<<y<<" "<<z<<endl; } void swap(point &p){ std::swap(x,p.x); std::swap(y,p.y); std::swap(z,p.z); cout<<"inner swap"<<endl; }private: int x,y,z;};void swap(point &x,point &y){ x.swap(y);} int _tmain(int argc, _TCHAR* argv[]){ point a(1,2,3); point b(4,5,6); cout<<"std swap"<<endl; std::swap(a,b); cout<<"boost swap"<<endl; boost::swap(a,b);}
聯(lián)系客服