欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

boost assign

程序员文章站 2022-06-01 09:30:17
...

#include<boost/assign.hpp>
#include<vector>
#include<set>
#include<string>
#include<utility>
#include<iostream>
using namespace std;
using namespace boost::assign;

template <typename T>
void print(const T& t)
{
typename T::const_iterator iter;
for(iter=t.begin();iter!=t.end();++iter)
cout << *iter << " ";
cout << endl;
}

template<>
void print(const map<int,string>& m)
{
map<int,string>::const_iterator iter;
for(iter=m.begin();iter!=m.end();++iter)
cout << iter->first << " " << iter->second << " ";
cout << endl;
}

int main(){
//operator+=很好用,但仅限于STL中定义的标准容器(vector,list,set...)
vector<int> v;
v += 1,2,3,4,5,6*6;
print(v);
set<string> s;
s += "cpp","java","c#","python";
print(s);

map<int,string> m;
m += make_pair(1,"one"),make_pair(2,"two");
print(m);

//push_back()
vector<int> v2;
push_back(v2)(1)(2)(3)(4)(5);
print(v2);

//push_front
list<string> l;
push_front(l)("cpp")("java")("c#")("python");
print(l);

//insert
set<double> s2;
insert(s2)(3.14)(0.324)(2.32);
print(s2);

map<int,string> m2;
insert(m2)(1,"one")(2,"two");
print(m2);
}

1 2 3 4 5 36
c# cpp java python
1 one 2 two
1 2 3 4 5
python c# java cpp
0.324 2.32 3.14
1 one 2 two


也支持list_of

#include<boost/assign.hpp>
#include"printT.h"
#include<string>
#include<deque>
using namespace std;
using namespace boost::assign;
int main()
{
vector<int> v = list_of(1)(2)(3)(4)(5);
print(v);

deque<string> d = list_of("power")("bomb")("phazon")("suit");
print(d);

set<int> s = (list_of(10),20,30,40,50,60);
print(s);

map<int,string> m = list_of(make_pair(1,"one"))(make_pair(2,"two"));
print(m);
}

1 2 3 4 5
power bomb phazon suit
10 20 30 40 50 60
1 one 2 two