-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotesOnSets.cpp
More file actions
116 lines (80 loc) · 3.11 KB
/
NotesOnSets.cpp
File metadata and controls
116 lines (80 loc) · 3.11 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
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
Perfect! Let’s review **sets vs. sequences** and **set relationships** in the context of **C++**, with examples and which STL (Standard Template Library) containers you'd use.
---
## 🔹 **Set vs. Sequence in C++**
### ✅ Sets in C++
- **Unordered, no duplicates**
- Use when you care about **uniqueness** and **fast lookup**
```cpp
#include <set>
#include <unordered_set>
std::set<int> orderedSet = {3, 1, 4}; // auto-sorted: 1, 3, 4
std::unordered_set<int> fastSet = {3, 1, 4}; // no guaranteed order
orderedSet.insert(2); // Now: 1, 2, 3, 4
fastSet.count(3); // Fast check if 3 exists
```
### ✅ Sequences in C++
- **Ordered, allows duplicates**
- Use when **order matters** or you need **indexed access**
```cpp
#include <vector>
#include <list>
#include <deque>
std::vector<int> numbers = {1, 2, 2, 3}; // Duplicates allowed
numbers.push_back(4); // Adds 4 to the end
int x = numbers[0]; // Access via index
std::list<int> linked = {1, 2, 3}; // Doubly linked list
```
---
## 🔹 **Set Relationships in C++**
### 1️⃣ One-to-One (Injective)
Use `std::map` to store unique keys and unique values.
```cpp
#include <map>
std::map<int, std::string> studentToID; // studentID -> schoolEmail
studentToID[1001] = "alice@school.edu";
studentToID[1002] = "bob@school.edu";
```
- Each key maps to exactly one unique value
- Values *should* be unique too, but C++ `map` doesn’t enforce it
---
### 2️⃣ One-to-Many
Use `std::map<int, std::vector<std::string>>`
```cpp
std::map<int, std::vector<std::string>> teacherToStudents;
teacherToStudents[101].push_back("Alice");
teacherToStudents[101].push_back("Bob");
```
- One teacher ID maps to **multiple students**
- Values stored as vectors or sets
---
### 3️⃣ Many-to-One
Use `std::unordered_map<std::string, int>`
```cpp
std::unordered_map<std::string, int> studentToCounselor;
studentToCounselor["Alice"] = 501;
studentToCounselor["Bob"] = 501; // Same counselor ID
```
- Multiple keys map to the same value
---
### 4️⃣ Many-to-Many
Use `std::unordered_map<std::string, std::unordered_set<std::string>>`
```cpp
std::unordered_map<std::string, std::unordered_set<std::string>> studentToClubs;
studentToClubs["Alice"].insert("Chess Club");
studentToClubs["Alice"].insert("Drama Club");
studentToClubs["Bob"].insert("Drama Club");
```
- Each student joins multiple clubs
- Each club can have multiple students (you’d create another map for club → students)
---
## 🔁 Summary Table (C++)
| Relationship Type | STL Container(s) |
|-------------------|------------------|
| Set | `std::set`, `std::unordered_set` |
| Sequence | `std::vector`, `std::list`, `std::deque` |
| One-to-One | `std::map` |
| One-to-Many | `std::map<Key, std::vector<Value>>` |
| Many-to-One | `std::unordered_map<Value, Key>` |
| Many-to-Many | `std::unordered_map<Key, std::unordered_set<Value>>` |
---
Want to go over how to write a class to manage any of these mappings, or how this ties into Discrete Math concepts like injective/surjective functions?