Thursday, 30 September 2021

Chapter 21 // Exercise 1 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 21 // Exercise 1

Go through the chapter and do all Try This exercises that you haven't already done.

Try This Pg 761

Github: 

To see if they were truly logically different, I went into debug mode and then compared the disassembly created.

You can see that Visual Studio produced identical code, however that is in debug mode. In release mode, it was slightly different:
The first implementation is faster and could be more optimised due to the NOP instruction which I believe aligns the loop in "jump chunks" (very brief explanation because I don't fully understand yet) which can help with instruction fetches. Both loops are doing basically the same thing though:
  • setting the conditional jump
  • comparing the contents of the iterator with 4
  • jumping back to the top while the condition is not equal and increasing the iterator
Try This Pg 765

Github: N/A

1. The value could be changed without you knowing, potentially breaking a part of the code.
2. Someone could change the predicate to use a different name value.
3. Someone could try an make the value global to access it in other places; or start using it where they're not supposed to.
I'd hate to see it in any application.

Try This Pg 774

Github: 

.

Try This Pg 785

Github: 

This one confused me a bit as on pg 783 he puts {"AA"] = "Alcoa Inc."} in the constructor of the map dow_name. This wouldn't compile in VS2019 and gave errors due to the '] ='. 

Also, the code didn't run initially when using inner_product() given on pg784. It asserted with the error "cannot dereference end map/set iterator". Turns out that Bjarne is a sneaky fox. The lines:
double alcoaprice = dowPrice["AAA"];
double bowingPrice = dowPrice["BA"];
ADD to the map if those keys are not found so my dowPrice map became a size of 5 whereas dowWeight was still 3. This caused the dereferencing error on the map iterator. I will not forget ever again that map will default add an element if it doesn't exist.

Try This Pg 787

Github: 

I'm pretty sure I'm running with at least C++17 so this worked. The order was different.

Try This Pg 792

Github: 

I set max_size to INT_MAX and that still caused an error. For this the overflow caused an exception so it was quite bad and I would not be tempted to ship it.


Tuesday, 28 September 2021

Chapter 21 // Drill 3 - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 21 // Drill 3.1

Read some floating-point values (at least 16 values) from a file into a vector<doubles> called vd.

Chapter 21 // Drill 3.2

Output vd to cout.

Chapter 21 // Drill 3.3

Make a vector vi of type vector<int> with the same number of elements as vd; copy the elements from vd into vi.

Chapter 21 // Drill 3.4

Output the pairs of (vd[i], vi[i]) to cout, one pair per line.

Chapter 21 // Drill 3.5

Output the sum of the elements of vd.

Chapter 21 // Drill 3.6

Output the difference between the sum of the elements of vd and the sum of the elements of vi.

Chapter 21 // Drill 3.7

There is a standard library algorithm called reverse that takes a sequence (pair of iterators) as arguments; reverse vd, and output vd to cout.

Chapter 21 // Drill 3.8

Compute the mean value of the elements in vd; output it.

Chapter 21 // Drill 3.9

Make a new vector<double> called vd2 and copy all elements of vd with values lower than (less than) the mean into vd2.

Chapter 21 // Drill 3.10

Sort vd, output it again.


Monday, 27 September 2021

Chapter 21 // Drill 2 - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 21 // Drill 2.1

Define a map<string, int> called msi.

Chapter 21 // Drill 2.2

Insert ten (name, value) pairs into it, e.g., msi["lecture"]=21.

Chapter 21 // Drill 2.3

Output the (name, value) pairs to cout in some format of your choice.

Chapter 21 // Drill 2.4

Erase the (name, value) pairs from msi.

Chapter 21 // Drill 2.5

Write a function that reads value pairs from cin and places them in msi.

Chapter 21 // Drill 2.6

Read 10 pairs from input and enter them into msi.

Chapter 21 // Drill 2.7

Write the elements of msi to cout.

Chapter 21 // Drill 2.8

Output the sum of the (integer) values in msi.

Chapter 21 // Drill 2.9

Define a map<int, string> called mis.

Chapter 21 // Drill 2.10

Enter the values from msi into mis; that is, if msi has an element ("lecture",21), mis should have an element (21, "lecture").

Chapter 21 // Drill 2.11

Output the elements of mis to cout.


For Drill 2.4 I wasn't sure if he meant erase a few or erase all so I went with erase all.

With Drill 2.7 I think he meant create an overload of the output operator for the map as creating a print function has already been done.

Whilst doing 2.9 I decided to make my functions more generic so they could accept maps of almost any type.

Sunday, 26 September 2021

Chapter 21 // Drill 1 - 8 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 21 // Drill 1.8

Repeat the exercise with a list<Item> rather than a vector<Item>.


I had to do two minimal things to get this work;
1 - Use advance instead of It + num.
2 - Use the sort() provided by the list container instead of just the standard one.

Saturday, 25 September 2021

Chapter 21 // Drill 1 - 1, 2, 3, 4, 5, 6, 7 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2019 and a modified version of the std_lib_facilities header found here.

Chapter 21 // Drill 1.1

Define struct Item { string name; int iid; double value; /* ... */};, make a vector<Item>, vi, and fill it with ten items from a file.

Chapter 21 // Drill 1.2

Sort vi by name.

Chapter 21 // Drill 1.3

Sort vi by iid.

Chapter 21 // Drill 1.4

Sort vi by value; print it in order of decreasing value (i.e., largest value first).

Chapter 21 // Drill 1.5

Insert Item{"horse shoe", 99, 12.34} and Item{"Canon S400", 9988, 499.95}.

Chapter 21 // Drill 1.6

Remove (erase) two Items identified by name from vi.

Chapter 21 // Drill 1.7

Remove (erase) two Items identified by iid from vi.

Github: 

New things all around in this exercise. I used the code from page 791 to start with and added operator>> to Item so I didn't have to modify it. This worked perfectly and vector happily constructed 10 Items using the data from the text file thanks to the operator overload. The input code is a bit hardcoded however it allows Items to have spaces in the name value as it uses getline().

For the second drill I tried my hand at creating a function object to sort the name. They are very easy to use.

For sorting by value I allowed a bool to passed to the function object which allows it to switch between increasing/decreasing order.

Whilst doing Drill 6 I also found out that std::remove_if will erase a member of a custom object when passed a predicate to find that member; very cool.