Section 6 Flashcards

1
Q

What are iterators?

A

iterate through data structures by pointing to them

iterating is the technical term for looping

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

Create a vector and iterate through it

A

include <vector></vector>

using namespace std;

vector<string> cars = {"Chevy", "Ford", "Mazda", "Volvo"};</string>

int main() {
vector<string>::iterator it; //The iterator is called "it"
// loop through vector
for(it = cars.begin(); it != cars.end(); ++it)
cout *it << "\n"
}</string>

*it - dereference operator accesses the element the iterator points to.

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

What is begin() and end()?

A

Functions that belong to data structures. They don’t belong to the iterator itself.
They are used with iterators to access and iterate through the elements of these data structures.

begin() - returns an iterator that points to first element
end() returns an iterator that points to one position after the last element

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

using begin, how would you point to the second element of a list?

A

it = cars.begin() + 1

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

Point to the third element using begin

A

it = cars.begin() + 2;

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

Using end, point to the last element of a list

A

it = cars.end() - 1

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

Why is the term point used with begin() and end()

A

Because they are like pointers. They point to elements in the data structure rather than returning values from them.
Basically it’s not making a copy, this is the real deal, homie

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

If you don’t want to declare an iterator and specify a specific type, what is an all encompassing word you can use with it?

A

Instead of:
vector<string>::iterator it = cars.begin();</string>

you can do this
auto it = cars.begin();

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

use an all encompassing word to specify the data type in a for loop to iterate

A

for (auto it = cars.begin(); it != cars.end(); ++it) {
cout &laquo_space;*it &laquo_space;“\n”;
}

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

What’s the difference between a for-each loop and an iterator?

A

for-each - when you are just reading the elements and don’t need to modify them.

iteration - when you need to add, modify, or delete elements during iteration, iterate in reverse, or skip elements.

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

Create a vector with cars, one of which is a bmw.

iterate through them and when it get’s to bmw, erase it.

A

// Create a vector called cars that will store strings
vector<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};</string>

// Loop through vector elements
for (auto it = cars.begin(); it != cars.end(); ) {
if (*it == “BMW”) {
it = cars.erase(it); // Remove the BMW element
} else {
++it;
}
}

// Print vector elements
for (const 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
12
Q

Iterate through a vector in reverse.

A

for (auto it = cars.rbegin(); it != cars.rend(); ++it) {
cout &laquo_space;*it &laquo_space;“\n”;
}

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

Create a map and iterate through it

A

// Create a map that will store strings and integers
map<string, int> people = { {“John”, 32}, {“Adele”, 45}, {“Bo”, 29} };

// Loop through the map with an iterator
for (auto it = people.begin(); it != people.end(); ++it) {
cout &laquo_space;it->first &laquo_space;” is: “ &laquo_space;it->second &laquo_space;“\n”;
}

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

What are algorithms?

A

They’re used to manipulate data in data structures

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

Create a vector of cars,
sort through them with an algorithm

A

include <iostream></iostream>

#include <vector>
#include <algorithm> // Include the <algorithm> library
using namespace std;</algorithm></algorithm></vector>

int main() {
// Create a vector called cars that will store strings
vector<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};</string>

// Sort cars in alphabetical order
sort(cars.begin(), cars.end());

// Print cars in alphabetical order
for (string car : cars) {
cout &laquo_space;car &laquo_space;“\n”;
}

return 0;
}

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

Sort a vector of numbers in reverse order

A

include <iostream></iostream>

#include <vector>
#include <algorithm>
using namespace std;</algorithm></vector>

int main() {
// Create a vector called numbers that will store integers
vector<int> numbers = {1, 7, 3, 5, 9, 2};</int>

// Sort numbers numerically in reverse order
sort(numbers.rbegin(), numbers.rend());

for (int num : numbers) {
cout &laquo_space;num &laquo_space;“\n”;
}

return 0;
}

17
Q

What values does sort() take?

A

the iterators begin() and end()

18
Q

sort a vector of numbers from the fourth element to the last

A

// Create a vector called numbers that will store integers
vector<int> numbers = {1, 7, 3, 5, 9, 2};</int>

// Sort numbers numerically, starting from the fourth element (only sort 5, 9, and 2)
sort(numbers.begin() + 3, numbers.end());

19
Q

Use an algorithm that looks for a number that you give it.
Create an if statement that prints something if it finds the number

A

include <iostream></iostream>

#include <vector>
#include <algorithm>
using namespace std;</algorithm></vector>

int main() {
// Create a vector called numbers that will store integers
vector<int> numbers = {1, 7, 3, 5, 9, 2};</int>

// Search for the number 3
auto it = find(numbers.begin(), numbers.end(), 3);

// Check if the number 3 was found
if (it != numbers.end()) {
cout &laquo_space;“The number 3 was found!” &laquo_space;“\n”;
} else {
cout &laquo_space;“The number 3 was not found.” &laquo_space;“\n”;
}

return 0;
}

.end() is returned if the number isn’t found

20
Q

Search for the first element that is greater than a specific value

A

// Create a vector called numbers that will store integers
vector<int> numbers = {1, 7, 3, 5, 9, 2};</int>

// Sort the vector in ascending order
sort(numbers.begin(), numbers.end());

// Find the first value that is greater than 5 in the sorted vector
auto it = upper_bound(numbers.begin(), numbers.end(), 5);

lower_bound would be used if looking for a number below 5

cout *it;

21
Q

Find the smallest element in a vector

A

// Create a vector called numbers that will store integers
vector<int> numbers = {1, 7, 3, 5, 9, 2};</int>

// Find the smallest number
auto it = min_element(numbers.begin(), numbers.end());

cout &laquo_space; *it;

22
Q

Show the largest number in a vector

A

// Create a vector called numbers that will store integers
vector<int> numbers = {1, 7, 3, 5, 9, 2};</int>

// Find the largest number
auto it = max_element(numbers.begin(), numbers.end());

cout &laquo_space;*it;

23
Q

Copy an element from one vector to another

A

using namespace std;

int main() {
// Create a vector called numbers that will store integers
vector<int> numbers = {1, 7, 3, 5, 9, 2};</int>

// Create a vector called copiedNumbers that should store 6 integers
vector<int> copiedNumbers(6);</int>

// Copy elements from numbers to copiedNumbers
copy(numbers.begin(), numbers.end(), copiedNumbers.begin());

// Print elements of copiedNumbers
for (int num : copiedNumbers) {
cout &laquo_space;num &laquo_space;“\n”;
}

return 0;
}

24
Q

Create a vector that will store 6 elements.
Fill all 6 with the value 35 using an algorithm

A

include <iostream></iostream>

#include <vector>
#include <algorithm>
using namespace std;</algorithm></vector>

int main() {
// Create a vector called numbers that will store 6 integers
vector<int> numbers(6);</int>

// Fill all elements in the numbers vector with the value 35
fill(numbers.begin(), numbers.end(), 35);

// Print all elements in the vector
for (int num : numbers) {
cout &laquo_space;num &laquo_space;“\n”;
}

return 0;
}

25
Q

Create a map and loop through it with a for-each loop, not an iterator.

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”;
}