Showing posts with label chapter 19 drills. Show all posts
Showing posts with label chapter 19 drills. Show all posts

Wednesday, 23 December 2020

Chapter 19 // Drill 14 - Principles & Practice Using C++

In these exercises I am using Visual Studio 2017 and the std_lib_facilities header provided by Stroustrup.

Chapter 19 // Drill 14

Bonus: Define input and output operators (>> and  <<) for vector<T>s. For both input and output use a { val, val, val } format. That will allow read_val() to also handle the S<vector<int>> variable.


This drill. I was like "sure no probs". Had it working perfectly for ints and then I remembered that this function can take in vectors of any type. Throw strings, floats, chars etc., at it and it breaks spectacularly. I then had to reset my brain to get into the mindset of generic programming...You need to think of everything and anything that could happen.



Apart from not being able to handle vectors of custom types (what a nightmare), it also doesn't handle strings with spaces in them. But I'm pretty happy with this. He chose this input/output specifically because it's really annoying. Bjarne is the true sneaky fox. If you needed very specific string output though I don't think you would use generic types.

Chapter 19 // Drill 14 - Principles & Practice Using C++

For this it checks if there are any undesirable characters and if not, it then reads the entire entry into a string. It removes commas from the string then converts it to a stringstream to be read into the templated type. This way, we don't have to bother with string to random type conversions; stringstream will take care of it for us. It also has the advantage of reading the entire variable in. So { 1.77, 5 }  will be read as:
- cin >> string, ignore { and ' ' 
- cin >> string (reads 1.77, )
- removes the comma
- stringstream >> type ( 1.77 is converted to type )
- push value into vector
- cin >> string, ignore space
- cin >> string ( reads 5 )
- no comma to remove
- stringstream >> type ( 5 is converted to type )
- push value into vector
- cin >> string, ignore space
- cin >> string, } read, break out of loop and return

Tuesday, 22 December 2020

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

In these exercises I am using Visual Studio 2017 and the std_lib_facilities header provided by Stroustrup.


Chapter 19 // Drill 1

Define template<typename T> struct S { T val; };.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2019/Drills/Drill%201

.

Chapter 19 // Drill 2

Add a constructor, so that you can initialise with a T.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2019/Drills/Drill%202

.

Chapter 19 // Drill 3

Define variables of types S<int>, S<char>, S<double>, S<string>, and S<vector<int>>; initialise them with values of your choice.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2019/Drills/Drill%203

.

Chapter 19 // Drill 4

Read those values and print them.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2019/Drills/Drill%204

 I assume he meant read them in using cin?

Chapter 19 // Drill 5

Add a function template get() that returns a reference to val.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2019/Drills/Drill%205

.

Chapter 19 // Drill 6

Put the definition of get() outside the class.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2019/Drills/Drill%206

.

Chapter 19 // Drill 7

Make val private. 

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2019/Drills/Drill%207

.

Chapter 19 // Drill 8

Do 4 again using get().

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2019/Drills/Drill%207

I guess he didn't mean to use cin earlier...literally just print them? Why use read? See code for drill 7 as this does the same thing.

Chapter 19 // Drill 9

Add a set() function template so that you can change val.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2019/Drills/Drill%209

.

Chapter 19 // Drill 10

Replace set() with an S<T>::operator=(const T&). Hint: Much simpler than Section 19.2.5.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2019/Drills/Drill%2010

.

Chapter 19 // Drill 11

Provide const and non-const versions of get().


.

Chapter 19 // Drill 12

Define a function template<typename T> read_val(T& v) that reads from cin into v.


Ok, so he definitely didn't mean to read in values in drill 4. I provided the operator overload version in 4 but I implemented the function here as well.

Chapter 19 // Drill 13

Use read_val() to read into each of the variables from 3 except the S<vector<int>> variable.

Github: .

See drill 12 as I did that there...I also already read into the vector...