Lesson 5: Standard Library Containers and Algorithms
Activity 19: Storing User Accounts
- First, we include the header files for the array class and input/output operations with the required namespace:
#include <array>
- An array of ten elements of type int is declared:
array<int,10> balances;
- Initially, the values of the elements are undefined since it is an array of the fundamental data type int. The array is initialized using a for loop, where each element is initialized with its index. The operator size() is used to evaluate the size of the array and the subscript operator [ ] is used to access every position of the array:
for (int i=0; i < balances.size(); ++i)
{
balances[i] = 0;
}
- We now want to update the value for the first and last user. We can use front() and back() to access the accounts of these users:
balances.front() += 100;
balances.back() += 100;
We would like to store the account balance of an arbitrary number of users. We then want to add 100 users to the account list, with a balance of 500.
- We can use vector to store an arbitrary number of users. It is defined in the <vector> header:
#include <vector>
- Then, we declare a vector of type int. Optionally, we reserve enough memory to store the 100 users’ account by calling reserve(100) to avoid memory reallocation:
std::vector<int> balances;
balances.reserve(100);
- Finally, we modify the for loop to add the balance for the users at the end of the accounts vector:
for (int i=0; i<100; ++i)
{
balances.push_back(500);
}
Activity 20: Retrieving a User’s Balance from their Given Username
- Include the header file for the map class and the header for string:
#include <map>
#include <string>
- Create a map with the key being std::string and the value int:
std::map<std::string, int> balances;
- Insert the balances of the users inside map by using insert and std::make_pair. The first argument is the key, the second one is the value:
balances.insert(std::make_pair(“Alice”,50));
balances.insert(std::make_pair(“Bob”, 50));
balances.insert(std::make_pair(“Charlie”, 50));
- Use the find function providing the name of the user to find the position of the account in the map. Compare it with end() to check whether a position was found:
auto donaldAccountPos = balances.find(“Donald”);
bool hasAccount = (donaldAccountPos != balances.end());
std::cout << “Donald has an account: “ << hasAccount << std::endl;
- Now, look for the account of Alice. We know Alice has an account, so there is no need to check whether we found a valid position. We can print the value of the account using ->second:
auto alicePosition = balances.find(“Alice”);
std::cout << “Alice balance is: “ << alicePosition->second << std::endl;
Activity 21: Processing User Registration in Order
- First, we include the header file for the stack class:
#include <stack>
- Create a stack providing the type to store:
std::stack<RegistrationForm> registrationForms;
- We start by storing the form inside the stack when the user registers. In the body of the storeRegistrationForm function, push the element into the queue:
stack.push(form);
std::cout << “Pushed form for user “ << form.userName << std::endl;
- Now, inside endOfDayRegistrationProcessing, we get all the elements inside the stack and then process them. Use the top() method to access the top element in the stack and pop() to remove the top element. We stop getting and removing the first element when no element is left:
while(not stack.empty()) {
processRegistration(stack.top());
stack.pop();
}
- Finally, we call our functions with some test data:
int main(){
std::stack<RegistrationForm> registrationForms;
storeRegistrationForm(registrationForms, RegistrationForm{“Alice”});
storeRegistrationForm(registrationForms, RegistrationForm{“Bob”});
storeRegistrationForm(registrationForms, RegistrationForm{“Charlie”});
endOfDayRegistrationProcessing(registrationForms);
}
Activity 22: Airport System Management
- We start by creating the class for Airplane. Make sure to first include the header for variant:
#include <variant>
- Then, create the class with a constructor that sets the current state of the airplane to AtGate:
class Airplane {
std::variant<AtGate, Taxi, Flying> state;
public:
Airplane(int gate) : state(AtGate{gate}) {
std::cout << “At gate “ << gate << std::endl;
}
};
- Now, implement the startTaxi() method. First, check the current state of the airplane with std::holds_alternative<>(), and if the airplane is not in the correct state, write an error message and return.
- If the airplane is in the correct state, change the state to taxi by assigning it to the variant:
void startTaxi(int lane, int numPassengers) {
if (not std::holds_alternative<AtGate>(state)) {
std::cout << “Not at gate: the plane cannot start taxi to lane “ << lane << std::endl;
return;
}
std::cout << “Taxing to lane “ << lane << std::endl;
state = Taxi{lane, numPassengers};
}
- We repeat the same process for the takeOff() method:
void takeOff(float speed) {
if (not std::holds_alternative<Taxi>(state)) {
std::cout << “Not at lane: the plane cannot take off with speed “ << speed << std::endl;
return;
}
std::cout << “Taking off at speed “ << speed << std::endl;
state = Flying{speed};
}
- We can now start looking at the currentStatus() method. Since we want to perform an operation for each of the states in the variant, we can use a visitor.
- Outside the Airplane class, create a class that has a method operator() for each of the types in the airplane state. Inside the method, print the information of the state. Remember to make the methods public:
class AirplaneStateVisitor {
public:
void operator()(const AtGate& atGate) {
std::cout << “AtGate: “ << atGate.gate << std::endl;
}
void operator()(const Taxi& taxi) {
std::cout << “Taxi: lane “ << taxi.lane << “ with “ << taxi.numPassengers << “ passengers” << std::endl;
}
void operator()(const Flying& flying) {
std::cout << “Flaying: speed “ << flying.speed << std::endl;
}
};
- Now, create the currentStatus() method and call the visitor on the state using std::visit:
void currentStatus() {
AirplaneStateVisitor visitor;
std::visit(visitor, state);
}
- We can now try to call the functions of Airplane from the main function:
int main()
{
Airplane airplane(52);
airplane.currentStatus();
airplane.startTaxi(12, 250);
airplane.currentStatus();
airplane.startTaxi(13, 250);
airplane.currentStatus();
airplane.takeOff(800);
airplane.currentStatus();
airplane.takeOff(900);
}