-
Notifications
You must be signed in to change notification settings - Fork 0
/
stl.cpp
123 lines (93 loc) · 2.4 KB
/
stl.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
#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
void printHeader() {
cout << endl << "#####################################" << endl;
}
void printFound(bool found) {
if (found) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
int main(int /*argc*/, char** /*argv*/) {
cout << endl << "Beginning STL Coding." << endl;
string aString("A");
string bString("B");
string cString("C");
string dString("D");
//
// Use a set
//
printHeader();
set<string> aSet;
aSet.insert(aString);
aSet.insert(bString);
aSet.insert(cString);
aSet.insert(cString);
cout << endl << "Set Contents:" << endl;
for (set<string>::iterator iter = aSet.begin();
iter != aSet.end();
iter++) {
cout << *iter << endl;
}
//
// Use a map
//
printHeader();
map<string, string> aMap;
aMap[aString] = bString;
aMap[cString] = dString;
cout << endl << "Map Contents:" << endl;
for (map<string, string>::iterator iter = aMap.begin();
iter != aMap.end();
iter++) {
cout << iter->first << " = " << iter->second << endl;
}
//
// Use a vector
//
printHeader();
vector<string> aVector;
aVector.push_back(aString);
aVector.push_back(bString);
cout << endl << "Vector Contents:" << endl;
for (vector<string>::iterator iter = aVector.begin();
iter != aVector.end();
iter++) {
cout << *iter << endl;
}
//
// Use a list
//
printHeader();
list<string> aList;
aList.push_back(aString);
aList.push_back(bString);
aList.push_back(cString);
cout << endl << "List Contents:" << endl;
for (list<string>::iterator iter = aList.begin();
iter != aList.end();
iter++) {
cout << *iter << endl;
}
//
// Testing binary search
//
printHeader();
sort(aVector.begin(), aVector.end());
cout << endl << "Is A in the vector?" << endl;
bool found = binary_search(aVector.begin(), aVector.end(), aString);
printFound(found);
cout << endl << "Is D in the vector?" << endl;
found = binary_search(aVector.begin(), aVector.end(), dString);
printFound(found);
printHeader();
cout << endl << "Done With STL Coding." << endl;
}