Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
C++ Fundamentals

You're reading from   C++ Fundamentals Hit the ground running with C++, the language that supports tech giants globally

Arrow left icon
Product type Paperback
Published in Mar 2019
Publisher
ISBN-13 9781789801491
Length 350 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
 Mallia Mallia
Author Profile Icon Mallia
Mallia
 Zoffoli Zoffoli
Author Profile Icon Zoffoli
Zoffoli
Arrow right icon
View More author details
Toc

Table of Contents (8) Chapters Close

About the Book 1. Getting Started FREE CHAPTER 2. Functions 3. Classes 4. Generic Programming and Templates 5. Standard Library Containers and Algorithms 6. Object-Oriented Programming 1. Appendix

Lesson 5: Standard Library Containers and Algorithms

Activity 19: Storing User Accounts

  1. First, we include the header files for the array class and input/output operations with the required namespace:

    #include <array>

  2. An array of ten elements of type int is declared:

    array<int,10> balances;

  3. 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;

    }

  4. 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.

  5. We can use vector to store an arbitrary number of users. It is defined in the <vector> header:

    #include <vector>

  6. 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);

  7. 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

  1. Include the header file for the map class and the header for string:

    #include <map>

    #include <string>

  2. Create a map with the key being std::string and the value int:

    std::map<std::string, int> balances;

  3. 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));

  4. 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;

  5. 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

  1. First, we include the header file for the stack class:

    #include <stack>

  2. Create a stack providing the type to store:

    std::stack<RegistrationForm> registrationForms;

  3. 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;

  4. 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();

    }

  5. 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

  1. We start by creating the class for Airplane. Make sure to first include the header for variant:

    #include <variant>

  2. 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;

        }

    };

  3. 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.
  4. 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};   

    }

  5. 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};

    }

  6. 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.
  7. 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;

      }

    };

  8. Now, create the currentStatus() method and call the visitor on the state using std::visit:

    void currentStatus() {

        AirplaneStateVisitor visitor;

        std::visit(visitor, state);

    }

  9. 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);

    }

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Visually different images