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
推荐阅读
-
Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object
-
JavaScript 复制对象与Object.assign方法无法实现深复制
-
【原创】自己手写实现Boost序列化简易版
-
Boost C++ 库 中文教程(全)
-
iOS---OBJC_ASSOCIATION_ASSIGN可能引起的Crash
-
C++Boost序列化(Serialization)库教程
-
阿迪BOUNCE和BOOST的差别,看了你就知道了
-
c/c++ 多线程 ubuntu18.04 boost编译与运行的坑
-
玩游戏超爽!OPPO Reno全系搭载Game Boost 2.0
-
ES 21 - Elasticsearch的高级检索语法 (包括term、prefix、wildcard、fuzzy、boost等)