-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.cpp
135 lines (106 loc) · 2.74 KB
/
container.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Container.
Reference:
http://www.topcoder.com/tc?module=Static&d1=tutorials&d2=standardTemplateLibrary2#creating
*/
//#define _GLIBCXX_DEBUG
#include <string>
#include <vector>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <cmath>
#include <queue>
#include <list>
#include "cout.h"
using namespace std;
#define Tr(c, i) for(typeof((c).begin()) i= (c).begin(); (i) != (c).end(); ++(i))
#define All(a) (a).begin(), (a).end()
bool pred(pair<string, int> a, pair<string, int> b) {
return a.second < b.second;
}
int map_iterate_sample() {
map<string, int> m;
m["suzuki"] = 12;
m["takuya"] = 18;
m["noriko"] = 26;
vector< pair<string, int> > v(All(m));
sort(All(v), pred);
Tr(m, i) {
cout << (i->first) << " : " << (i->second) << endl;
}
Tr(v, i) {
cout << (i->second) << " : " << (i->first) << endl;
}
return 0;
}
int next_permutaiton_sample() {
int ary[4];
ary[0] = 0, ary[1] = 1, ary[2] = 2, ary[3] = 3;
set<string> s;
vector<string> vs;
vs.push_back("a"), vs.push_back("b"), vs.push_back("c"), vs.push_back("d");
sort(All(vs));
do {
string t;
t = vs[ary[0]] + vs[ary[1]] + vs[ary[2]] + vs[ary[3]];
s.insert(t);
} while (next_permutation(ary, ary+4));
int c = s.size();
assert(c == 24);
return 0;
}
int element_sample() {
vector<int> v;
v.push_back(1);v.push_back(10);v.push_back(-39);v.push_back(5);
assert(-39 == *min_element(All(v)));
assert(10 == *max_element(All(v)));
sort(All(v));
assert(binary_search(All(v), 5));
/* upper_bound returns a itereator of next to upper limit */
/* See http://topcoder.g.hatena.ne.jp/nitoyon/20081025/1224944902 */
/* 6以上になるはじめての場所 */
assert(5 == *(upper_bound(All(v), 6) - 1));
/* 1以下の最大の数字 */
assert(1 == *(lower_bound(All(v), 1)));
v.push_back(10);
assert(10 == *adjacent_find(All(v)));
assert(2 == count(All(v), 10));
cout << "passed min_element_sample" << endl;
return 1;
}
int count_continue() {
int a[] = {1, 3, 5, 2, 4, 10, 2, 2};
// got error: int n = (int)sizeof(a);
int n = (int)(sizeof(a) / sizeof(int));
int c=0;
sort(a, a+n);
do {
if (adjacent_find(a, a+n) != a + n) {
++c;
}
}while(next_permutation(a, a+n));
assert(c == 4320);
return 0;
}
int erase_sample() {
vector<int> v;
v.push_back(2); v.push_back(5); v.push_back(3); v.push_back(9);
v.push_back(12); v.push_back(1); v.push_back(12); v.push_back(8);
assert(v.size() == 8);
sort(All(v));
v.erase(unique(All(v)), v.end());
assert(v.size() == 7);
v.clear();
assert(v.size() == 0);
return 1;
}
int main() {
map_iterate_sample();
next_permutaiton_sample();
element_sample();
count_continue();
erase_sample();
return 0;
}