1. 程式人生 > >boost實用工具:assign庫了解學習

boost實用工具:assign庫了解學習

學習 cin.get text add repeat sign solid ica return

  許多時候,我們需要為容器初始化或者賦值,填入大量的數據;
  STL容器僅提供了容納這些數據的方法,但是填充的步驟是相當地麻煩(insert、push_back);
  於是,boost::assign出現了,其重載了+= ,()等運算符,用難以想象的簡潔對STL容器初始化或賦值.
  +=很好用,但是僅適用於STL的標準容器,對於boost新容器卻無能為力;
  ()可以更靈活實現對容器的初始化;
  容器構造的時候就對數據進行填充list_of等.

C++ Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
許多時候,我們需要為容器初始化或者賦值,填入大量的數據;
STL容器僅提供了容納這些數據的方法,但是填充的步驟是相當地麻煩(insert、push_back);
於是,boost::assign出現了,其重載了+= ,()等運算符,用難以想象的簡潔對STL容器初始化或賦值.

+=很好用,但是僅適用於STL的標準容器,對於boost新容器卻無能為力;
()可以更靈活實現對容器的初始化;
容器構造的時候就對數據進行填充list_of等.
*/


/************************************************************************/

/* C++ stl Library */
/************************************************************************/
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>

/************************************************************************/

/* C++ boost Library */
/************************************************************************/
#include "boost/assign.hpp"

using namespace boost::assign;
using namespace std;

int main(void)
{
//operator+=
vector<int> vec;
vec +=
1,2,3,4,5,6*6;

set<string> s;
s +=
"c","c++","java","c#";

map<
int,string> mInfo;
mInfo += make_pair(
1,"Michael"),make_pair(2,"James");

//operator()
vector<int> v1;
push_back(v1) (
1) (2) (3) (4) (5);

map<
int,string> maps;
insert(maps) (
1,"East") (2,"West");

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

//list_of map_list_of/pair_list_of (tuple_list_of)
vector<int> vec1 = list_of(1) (2) (3) (4) (5);
set<
int> set1 = (list_of(10), 20,30,40,50);
map<
int,int> map1 = map_list_of(1,1) (2,2) (3,3);
map<
int,string> map2 = pair_list_of(1,"aaa") (2,"bbb");

//減少重復輸入 repeat repeat_fun range
vector<int> vv = list_of(1).repeat(3,2) (4) (5) (6);

//更多內容請感興趣研究
//list_of的嵌套使用list_of(list_of())
//引用初始化列表ref_list_of() cref_list_of()
//...

cin.get();
return 0;
}

boost實用工具:assign庫了解學習