maneras de accesar contenedores asociativos como maps y sets
contenedores-asociativos.cpp
edited
1#include <iostream>
2#include <string>
3#include <map>
4#include <unordered_set>
5#include <set>
6
7using namespace std;
8int main()
9{
10 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ map
11 cout << "map testing: " << endl;
12 map<string, int> m = {
13 {"meow", 2},
14 {"meow2", 4}};
15
16 // inserting
17 m.insert({"another meow", 10});
18
19 // accesing by key
20 cout << m.at("meow") << endl; // if key doesn't exist, it will throw an error while compiling
21 cout << m["meow3"] << endl; // if key doesn't exist, it will insert it into the map
22
23 cout << endl;
24
25 if (m.find("meow3") != m.end()) // si encuantra la llave "meow3"
26 cout << "\"meow3\" value: " << m.find("meow3")->second << endl; // la imprime
27 cout << endl;
28
29 // for loop
30 for (auto it = m.begin(); it != m.end(); it++)
31 {
32 cout << "[key] : " << it->first << " | value : " << it->second << endl;
33 }
34 cout << endl;
35
36 // for range based loop
37 for (auto &p : m)
38 cout << "[key] : " << p.first << " | value : " << p.second << endl;
39 cout << endl;
40
41 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ multimap
42 cout << "multimap testing: " << endl;
43 multimap<string, double> multi = {
44 {"des", 3.2},
45 {"des", 5.4},
46 {"helena", 6.3},
47 {"des", 1.2}};
48
49 // accessing by key (don't do this with multimaps, it will only print the first key it finds)
50 if (multi.find("helena") != multi.end())
51 {
52 cout << "helena value: " << multi.find("helena")->second << endl;
53 }
54
55 // for loop (con ->)
56 for (auto it = multi.begin(); it != multi.end(); it++)
57 {
58 cout << "[key] : " << it->first << " | value : " << it->second << endl;
59 }
60 cout << endl;
61
62 // for loop (it dereferenced)
63 for (auto it = multi.begin(); it != multi.end(); it++)
64 {
65 cout << "[key] : " << (*it).first << " | value : " << (*it).second << endl;
66 }
67 cout << endl;
68
69 // using bounds para solo imprimir las llaves "des"
70 cout << "valores de las llaves \"des\" : " << endl;
71 for (auto it = multi.lower_bound("des"); it != multi.upper_bound("des"); it++)
72 cout << it->second << endl;
73
74 cout << endl;
75 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ sets
76 cout << "set testing: " << endl;
77
78 set<int> s = {4, 3, 6, 7, 1};
79 s.insert(2);
80
81 // for loop
82 for (auto it = s.begin(); it != s.end(); it++)
83 cout << " " << *it;
84 cout << endl;
85
86 // for range based loop
87 for (auto &i : s)
88 {
89 cout << " " << i;
90 }
91 cout << endl;
92 cout << endl;
93 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ unordered sets
94 cout << "unordered_set testing: " << endl;
95
96 unordered_set<int> u = {4, 3, 6, 7, 1};
97 u.insert(10);
98
99 // find (el método find no devuelve data, si no, es un iterador que apunta al nodo donde se encuantra la llave)
100 auto findMe = u.find(10);
101 if (findMe != u.end())
102 {
103 cout << "llave encontrada: " << *findMe << endl; // se le hace dereference
104 }
105 cout << endl;
106
107 for (auto it = u.begin(); it != u.end(); it++)
108 cout << " " << *it;
109 cout << endl;
110 cout << endl;
111
112 return 0;
113}