Friday, 29 March 2024

Chapter 4 // Exercises 1-10 - The C++ Programming Language

 For this exercise I'm using Visual Studio Community 2022 and the header file std_lib_facilities:

The exercises can be found online and are not actually in the book:

Chapter 4 - A Tour of C++: Containers and Algorithms
Exercise 1
When first reading this chapter, keep a record of information that was new or surprising to you. Later, use that list to focus your further studies.

1. I didn't know that for_each() is classed as an algorithm. The more you know.

Exercise 2
List 5 standard library containers.

1. std::vector
2. std::array
3. std::map
4. std::deque
5. std::list

Exercise 3
List 5 standard library algorithms.

1. for_each()
2. find()
3. search()
4. copy()
5. remove()

Exercise 4
List 5 standard library headers.

1. #include <string>
2. #include <list>
3. #include <iostream>
4. #include <vector>
5. #include <random>

Exercise 5
Write a program that reads a name (a string) and an age (an int) from the standard input stream cin. Then output a message including the name and age to the standard output stream cout.


Exercise 6
Redo exercise 5, storing several (name, age) pairs in a class. Doing the reading and writing using your own >> and << operators.


Exercise 7
Initialise a vector<int> with the elements 5, 9, -1, 200 and 0. Print it. Sort it, and print it again.


Exercise 8
Repeat exercise 7 with a vector<string> initialised with "Kant", "Plato", "Aristotle", "Kierkegaard", and "Hume".


Exercise 9
Open a file for writing (as an ofstream) and write a few hundred integers to it.


Exercise 10
Open the file of integers from exercise 9 for reading (as an ifstream) and read it.













No comments:

Post a Comment