Monday, 4 October 2021

Chapter 21 // Exercise 5 - 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 5

What would we have to do if we couldn't return end() to indicate "not found"? Redesign and re-implement find() and count() to take iterators to the first and last elements. Compare the results to the standard versions.


Erm I think I did this back to front? I didn't return an iterator for count as the standard version doesn't it returns a std::iterator_traits<It>::difference_type, so that's what I did. Also, find() already takes in iterators to the first and last elements. After some thinking I realised he meant the last element not the end (which is 1 past the last element).

With count it's fairly simple as your count integer will just be at 0 to indicate nothing was found. For find, I made the return iterator an output parameter and the function now returns a bool so you can stick it in an if statement. Highly inefficient compared to it's std counterparts.

No comments:

Post a Comment