Section 6 Flashcards
What are iterators?
iterate through data structures by pointing to them
iterating is the technical term for looping
Create a vector and iterate through it
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.
What is begin() and end()?
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
using begin, how would you point to the second element of a list?
it = cars.begin() + 1
Point to the third element using begin
it = cars.begin() + 2;
Using end, point to the last element of a list
it = cars.end() - 1
Why is the term point used with begin() and end()
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
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?
Instead of:
vector<string>::iterator it = cars.begin();</string>
you can do this
auto it = cars.begin();
use an all encompassing word to specify the data type in a for loop to iterate
for (auto it = cars.begin(); it != cars.end(); ++it) {
cout «_space;*it «_space;“\n”;
}
What’s the difference between a for-each loop and an iterator?
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.
Create a vector with cars, one of which is a bmw.
iterate through them and when it get’s to bmw, erase it.
// 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 «_space;car «_space;“\n”;
}
Iterate through a vector in reverse.
for (auto it = cars.rbegin(); it != cars.rend(); ++it) {
cout «_space;*it «_space;“\n”;
}
Create a map and iterate through it
// 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 «_space;it->first «_space;” is: “ «_space;it->second «_space;“\n”;
}
What are algorithms?
They’re used to manipulate data in data structures
Create a vector of cars,
sort through them with an algorithm
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 «_space;car «_space;“\n”;
}
return 0;
}
Sort a vector of numbers in reverse order
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 «_space;num «_space;“\n”;
}
return 0;
}
What values does sort() take?
the iterators begin() and end()
sort a vector of numbers from the fourth element to the last
// 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());
Use an algorithm that looks for a number that you give it.
Create an if statement that prints something if it finds the number
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 «_space;“The number 3 was found!” «_space;“\n”;
} else {
cout «_space;“The number 3 was not found.” «_space;“\n”;
}
return 0;
}
.end() is returned if the number isn’t found
Search for the first element that is greater than a specific value
// 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;
Find the smallest element in a vector
// 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 «_space; *it;
Show the largest number in a vector
// 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 «_space;*it;
Copy an element from one vector to another
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 «_space;num «_space;“\n”;
}
return 0;
}
Create a vector that will store 6 elements.
Fill all 6 with the value 35 using an algorithm
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 «_space;num «_space;“\n”;
}
return 0;
}
Create a map and loop through it with a for-each loop, not an iterator.
map<string, int> people = { {“John”, 32}, {“Adele”, 45}, {“Bo”, 29} };
for (auto person : people) {
cout «_space;person.first «_space;” is: “ «_space;person.second «_space;“\n”;
}