Data structures part 2 Flashcards

1
Q

What is a set

A

Sorted in ascending order
unique values only
can add or remove but not modify
based on sorting not indexing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Create a set and print its elements

A

include <set></set>

set<string> cars;</string>

set<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};</string>

// Print set elements
for (string car : cars) {
cout &laquo_space;car &laquo_space;“\n”;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How are sets sorted

A

alphabetically or numerically
1234
abcd

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Sort a set in descending order

A

// Sort elements in a set in descending order
set<int, greater<int>> numbers = {1, 7, 3, 2, 5, 9};
// Print the elements
for (int num : numbers) {
cout << num << "\n";
}</int>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Add an element to a set

A

car.insert(“element”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Remove a specific element

A

cars.erase(“Volvo”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Remove all items from a set

A

cars.clear()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Check if a set is empty

A

cars.empty()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

loop through a set

A

set<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};</string>

for (string car : cars) {
cout &laquo_space;car &laquo_space;“\n”;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a map

A

Stores key:value pairs, so basically a dictionary
stored in ascending order

You will need to define the key and value data type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Create a map

A

include <map>

map<string, int> people = { {“John”, 32}, {“Adele”, 45}, {“Bo”, 29} };

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Access the value for a key named ‘john’ in a map called ‘people’

A

cout &laquo_space;people[“john”]

or cout &laquo_space;people.at(“John”)

at is preferred

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Add elements to a map

A

people[“Jenny”] = 22;

or

people.insert({“Jenny”, 32});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can you have two elements with equal keys?

A

No, values can be equal though

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Remove a specific element from a map

A

people.erase(“John”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Remove all elements from a map

A

people.clear()

17
Q

Show how many elements a map has

A

people.size()

18
Q

Check if the map is empty

19
Q

Show if the key “John” exists

A

people.count(“John”)

20
Q

Loop through a map

Now loop through these in reverse order

A

map<string, int> people = { {“John”, 32}, {“Adele”, 45}, {“Bo”, 29} };

for (auto person : people) {
cout &laquo_space;person.first &laquo_space;” is: “ &laquo_space;person.second &laquo_space;“\n”;
}

auto - auto detects data type
.first - for keys
.second - for values

map<string, int, greater<string>> people = { {"John", 32}, {"Adele", 45}, {"Bo", 29} };</string>

for (auto person : people) {
cout &laquo_space;person.first &laquo_space;” is: “ &laquo_space;person.second &laquo_space;“\n”;
}