forked from teseoch/CPP-Fall-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path69-more_maps.cpp
48 lines (38 loc) · 1.35 KB
/
69-more_maps.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
#include <iostream>
#include <map>
#include <string>
#include "stopwatch.hpp"
int main()
{
//Various ways to add new elements to a map
std::vector<int> bla{1, 1, 1};
// bla.push_back(1);
// bla.push_back(1);
// bla.push_back(1);
//Method 1: Add {key, value} pairs when creating the map
std::map<std::string, unsigned int> fruit_ratings{
{"Dragon fuit", 8},
{"Chesnut", 9},
{"Persimon", 0}};
//Method 2: Use .insert to add {key, value} pair objects
fruit_ratings.insert(std::pair<std::string, unsigned int>{"Cherry", 5});
fruit_ratings.insert(std::make_pair("Mago", 9));
fruit_ratings.insert({"Passionfruit", 10});
//Method 3: Use the square bracket operator
fruit_ratings["Pineapple"] = 6;
fruit_ratings["Pumpkin"] = 10;
//.at would not work
for (const auto &[name, rating] : fruit_ratings)
{
std::cout << name << ": " << rating << "/10" << std::endl;
}
std::map<std::string, std::vector<int>> fruit_ratingss{
{"Dragon fuit", {8, 8, 8}},
{"Chesnut", {9}},
{"Persimon", {0, 10}}};
std::map<std::string, std::pair<int, double>> test{};
std::map<std::vector<double>, Stopwatch> not_really_make_sense{
{{1.0, 2.0, 3.0}, Stopwatch{}}, {{1.0, 2.0, 2.0}, Stopwatch{}}};
not_really_make_sense.at({1.0, 2.0, 3.0});
return 0;
}