This repository was archived by the owner on Aug 11, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmapTests.cpp
More file actions
68 lines (60 loc) 路 1.98 KB
/
Copy pathmapTests.cpp
File metadata and controls
68 lines (60 loc) 路 1.98 KB
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
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
template<typename T>
void printMap(const T &m) {
for (const auto& it : m) {
cout << it.first << ": " << it.second << endl;
}
}
template<typename T>
void printMapV(const T &m) {
for (const auto& it : m) {
cout << it.first << ": ";
for (const auto &i : it.second) {
cout << i << " ";
}
cout << endl;
}
}
int main() {
cout << "----------- Test <string, string> map -----------" << endl;
map<string, string> langs;
langs["Python"] = "Guido van Rossum";
langs["Java"] = "James Gosling";
langs["Java"] = "James Gosling"; // all keys are unique
langs["C"] = "Brian Kernighan";
langs["C++"] = "Bjarne Stroustrup";
langs["JavaScript"] = "Brendan Eich"; // map is automatically sorted by key
printMap(langs);
cout << endl << "----------- Test <string, int> map -----------" << endl;
map<string, int> m = {{"third", 3}, {"second", 2}, {"first", 1}}; // map is automatically sorted by key
cout << "new map m:" << endl;
printMap(m);
cout << endl << "erase: " << m.find("second")->first << endl; // find() by the key, get value of the key
m.erase("second");
cout << "map m after erase:" << endl;
printMap(m);
cout << endl << "----------- Count quantity of values at vector with map -----------" << endl;
vector<string> v = {"one", "one", "two", "three", "four", "two", "one"};
map<string, int> counters;
for (const auto &it : v) {
// redundant
// if (counters.count(it) == 0) { // count returns how many times the key is found in the map
// counters[it] = 1;
// } else {
counters[it]++;
// }
}
printMap(counters);
cout << endl << "----------- Count quantity of values at vector with map by char key -----------" << endl;
map<char, vector<string>> groupedWords;
for (const string &word : v) {
if (find(groupedWords[word[0]].begin(), groupedWords[word[0]].end(), word) == groupedWords[word[0]].end())
groupedWords[word[0]].push_back(word);
}
printMapV(groupedWords);
return 0;
}