Wednesday, 17 March 2021

Chapter 20 // Drill 6, 7, 8 - Principles & Practice Using C++

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

Drill 6

Write a simple copy() operation,
template<typename Iter1, typename Iter2>
    // requires Input_iterator<iter1>() && Output_iterator<Iter2>()
Iter2 copy(Iter f1, Iter e1, Iter f2);
that  copies [f1, e1) to [f2, f2+(e1-f1)) and returns f2+(e1 - f1) just like the standard library copy function. Note that if f1==e1 the sequence is empty, so that there is nothing to copy.

Drill 7

Use your copy() to copy the array into the vector and to copy the list into the array.

Drill 8

Use the standard library function find() to see if the vector contains the value 3 and print out its position if it does; use find() to see if the list contains the value 27 and print out it's position if it does. The "position" of the first element  is 0, the position of the second element is 1, etc. Note that if find() returns the end of the sequence, the value wasn't found.

Github: 

In drill 7 I realised he meant to use a std:: array as a normal C array does not have iterators built in, so I changed it.

I did discover some nice std functions in these drills though. Namely distance() which returns the index of an iterator and find. I'm sure I've used find before but now I actually understand what it's doing "under the hood".

Tuesday, 16 March 2021

Chapter 20 // Drills 1, 2, 3, 4, 5 - Principles & Practice Using C++

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

Drill 1

Define an array of ints with the ten elements { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }.

Drill 2

Define a vector<int> with those ten elements.

Drill 3

Define a list<int> with those ten elements.

Drill 4

Define a second array, vector and list, each initialised as a copy of the first array, vector, and list, respectively.

Drill 5

Increase the value of each element in the array by 2; increase the value of each element in the vector by 3; increase the value of each element in the list by 5.

Github: 

I'm not sure if he meant a user defined array (as seen on pg 748), a std::array or an actual array[]. I went with the last one.

Monday, 8 March 2021

Chapter 19 // Exercise 16 - Principles & Practice Using C++

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

Chapter 19 // Exercise 16

Sometimes, it is desirable that an empty vector be as small as possible. For example, someone might use vector<vector<vector<int>>>  a lot but have most elements vectors empty. Define a vector so that sizeof(vector<int>) == sizeof(int*), that is, so that the vector itself consists only of a pointer to a representation consisting of the elements, the number of elements, and a space pointer.


I was a bit confused at first due to the wording as I thought he meant to have 2 pointers but it could only be the size of 1 pointer. But, after re-reading the question several times realised he meant for the vector representation to hold all three.

I basically reused the vector created in exercises 8 and 9 but took out the allocator and used new and delete instead. This created an entire evening of nightmares. If I didn't understand the difference between copy/move constructors and copy/move assignment before; I sure as hell do now.

I ended up using the crt debug library to track down the exact new that was causing me problems and I realised that my move assignment was not deleting properly. I've left the crt stuff in the code; it's an extremely useful tool.


I also became a pedantic about my vectors. I wanted the original vector to be a fully functional vector in it's own right and the SmallVector to just rely on all of OriginalVector's functions. I think that's what caused me the headache as I tried to separate in my head what was an allocation and what wasn't. 

Ultimately, a frustrating yet extremely rewarding exercise.

When I ran the code a vector<vector<vector<int>>> reported a size of 16 and my SmallVector<SmallVector<SmallVector<int>>> was 4.

And with that, Chapter 19 is finally over. I started it on the 22nd of December and it was beginning to feel like the chapter that would never end. That said I've been doing about 3000 other projects in-between. I really need to focus on this book more.

Sunday, 7 March 2021

Chapter 19 // Exercise 14, 15 - Principles & Practice Using C++

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

Chapter 19 // Exercise 14

Provide a GUI interface and a bit of graphical output to the "Hunt the Wumpus" game from the exercises in Chapter 18. Take the input in an input box and display a map of the part of the cave currently known to the player in a window.

Chapter 19 // Exercise 15

Modify the program from the previous exercise to allow the user to mark rooms based on knowledge and guesses, such as "maybe bats" and "bottomless pit."

Github: 

I was both happy and filled with dread at this exercise as my code had been hackily hard coded to suit a console window environment. I then almost gave up as I realised just how much code relied on _getch() pausing the window. After much drinking, I then remembered state machines are thing and hackily threw together a quick state system that forces the game to wait for presses. I then changed all the couts to a stringstream and pushed that to a new widget called Multiline_Outbox which was created from an Fl_Multiline_Outbox.

For the second part, I quickly added another input box where you can type "13p" or "13b" to mark it with a guess. There is no testing anywhere in this game for incorrect input so, put the right input in.

This is quite possibly some of the worst code I've ever written but I'm glad I didn't skip the exercises. I wanted to make it prettier but I also really want to move onto chapter 20.

Due to the amount of file changes I've uploaded the full project to github as well as the main files.

Chapter 19 // Exercise 14, 15  - Principles & Practice Using C++













Thursday, 4 March 2021

Chapter 19 // Exercise 13 - Principles & Practice Using C++

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

Chapter 19 // Exercise 13

Write a Tracer class where its constructor prints a string and its destructor prints a string. Give the strings as constructor arguments. Use it to see where RAII management objects will do their job (i.e., experiments with Tracers as local objects, member objects, global objects, objects allocated by new, etc.). Then add a copy constructor and a copy assignment so that you can use Tracer objects to see when copying is done.


You won't see the destructor calls for anything except the Tracer allocated by new because the rest are destroyed on return 0.

I did find out how to print line numbers in this exercise though. __LINE__ is a very handy macro. There are several others that you can use to print the file, time etc,.