forked from teseoch/CPP-Fall-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path59-sets.cpp
131 lines (112 loc) · 3.37 KB
/
59-sets.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
#include <iostream>
#include <set>
#include <string>
int main()
{
std::set<std::string> fruit{};
/* Add some elements to the set */
fruit.insert("Raspberry");
fruit.insert("Pear");
fruit.insert("Cranberry");
fruit.insert("Strawberry");
fruit.insert("Pineapple");
fruit.insert("Guava");
fruit.insert("Lemon");
fruit.insert("Orange");
fruit.insert("Apple");
/* Task 1: Print out the size */
std::cout << fruit.size() << std::endl;
// a < b
/* Task 2: Add the same element repeatedly and print the size again */
fruit.insert("Apple");
fruit.insert("Apple");
fruit.insert("Apple");
fruit.insert("Apple");
fruit.insert("Apple");
std::cout << fruit.size() << std::endl;
/* Task 3: Try iterating over the set with a for-each loop */
for (const auto &f : fruit)
{
std::cout << f << std::endl;
}
std::cout << std::endl;
std::cout << std::endl;
/* Task 4a: Search for the values "Lemon" and "Lime" using .contains */
if (fruit.contains("Lemon"))
{
std::cout << "lemon is there" << std::endl;
}
else
{
std::cout << "lemon is not there" << std::endl;
}
if (fruit.contains("Lime"))
{
std::cout << "Lime is there" << std::endl;
}
else
{
std::cout << "Lime is not there" << std::endl;
}
std::cout << std::endl;
std::cout << std::endl;
/* Task 4b: Search for the values "Lemon" and "Lime" using .count */
if (fruit.count("Lemon") == 1)
{
std::cout << "lemon is there" << std::endl;
}
else
{
std::cout << "lemon is not there" << std::endl;
}
if (fruit.count("Lime") == 1)
{
std::cout << "Lime is there" << std::endl;
}
else
{
std::cout << "Lime is not there " << fruit.count("Lime") << std::endl;
}
std::cout << std::endl;
std::cout << std::endl;
/* Task 4c: Search for the values "Lemon" and "Lime" using .find */
if (fruit.find("Lemon") != fruit.end())
{
std::cout << "lemon is there" << std::endl;
}
else
{
std::cout << "lemon is not there" << std::endl;
}
if (fruit.find("Lime") != fruit.end())
{
std::cout << "Lime is there" << std::endl;
}
else
{
std::cout << "Lime is not there" << std::endl;
}
std::cout << std::endl;
std::cout << std::endl;
/* Task 5: Attempt to delete the values "Lemon" and "Grapefruit" */
std::cout << "deleted " << fruit.erase("Lemon") << " Lemons" << std::endl;
std::cout << "deleted " << fruit.erase("Grapefruit") << " Grapefruits" << std::endl;
for (const auto &f : fruit)
{
std::cout << f << " ";
}
std::cout << std::endl;
/* Task 6: Insert the values "Lime" and "Raspberry", using the return
value of .insert to check whether the element was already
present */
// auto pair = fruit.insert("Lime");
// auto it1 = pair.first;
// auto bool1 = pair.second;
auto [it1, bool1] = fruit.insert("Lime");
auto [it2, bool2] = fruit.insert("Raspberry");
// std::cout << "Lime was succesfull? " << bool1 << std::endl;
// std::cout << "Raspberry was succesfull? " << bool2 << std::endl;
std::cout << *it1 << " was succesfull? " << bool1 << std::endl;
std::cout << *it2 << " was succesfull? " << bool2 << std::endl;
return 0;
}