Tutorial 7 Y. Bernat Object Oriented Programming 2, Spring 2016
Exercise 4 STL Outline Part I Today's Topics 1 Exercise 4 2 STL Containers - continue Lambda functions Algorithms
Exercise 4 STL Exercise 4 Let's read the exercise
Exercise 4 STL Containers - continue Lambda functions Algorithms Unordered associative containers std::unordered_map, std::unordered_set, std::unordered_multimap, std::unordered_multiset Performance
Exercise 4 STL Containers - continue Lambda functions Algorithms How to choose which container to use The most natural for the problem If there is a performance issue and the proler shows it's around the container access: Try converting to vector Use reserve() for better vector performance (But rst think about your algorithms and access patterns!)
Exercise 4 STL Containers - continue Lambda functions Algorithms Intro Not part of STL, part of the language (since 11, with great enhancements in 14) Needed as a tool for the next topic for STL, algorithms The problem: Sometimes it's very useful to get a function as a parameter The trivial choice is a function pointer Example code Initial solution: Functor (function object) Example code
Exercise 4 STL Containers - continue Lambda functions Algorithms Lambda functions Lambda parts: [] - Lambda function introducer (and capture list) Capture by value, by reference, this, default capturing init-capture (14) The parameter list (can be ommitted if empty; can use auto since 14) mutable The return type (trailing return type style) - usually not needed The (lambda) function body
Exercise 4 STL Containers - continue Lambda functions Algorithms Summary Lambda function is a functor that is written easily and inline All the logic is local, no need to nd the correct scope to dene the function when writing and nd the function when reading the code Don't overuse/abuse! If it's long, it's probably better as a regular function or functor
Exercise 4 STL Containers - continue Lambda functions Algorithms Overview Most of them in <algorithm>, some in <numeric>, and specic groups in <functional> or even <iterator> Implement common algorithms in a generic way Generic AKA using iterators What if we need an action to be done on the container? 3 options: 1 Specic algorithm version as a member (e.g. sort() and remove() for std::list) 2 Using iterator that knows how to do it (e.g. std::back_insert_iterator) 3 Giving us the needed info and we'll do it (e.g. std::remove())
Exercise 4 STL Containers - continue Lambda functions Algorithms Examples std::reverse() - Takes two iterators, rst and last, and replace every pair of values between rst and last-1 std::count() - Takes two iterators and a value and counts how many times the value appears in the range std::count_if() - Takes two iterators and a predicate and counts for how many values the predicate returns true std::all_of() / std::any_of() / std::none_of() - Take two iterators and a predicate and returns if the predicate holds for all/any/none of the values in the range. Note the dierences about empty range!
Exercise 4 STL Containers - continue Lambda functions Algorithms Examples 2 std::remove() - Takes two iterators and a value and removes this value from the range Returns the new end Be careful! It doesn't do what we'd think it does! It doesn't know anything about the container, so it just takes all the not-to-be-removed items to the front, optionally overwriting some to-be-removed items, and tells us where is the new end Now we can use the returned information to really delete the unneeded items Remove-erase idiom std::remove_if() - Of course...
Exercise 4 STL Containers - continue Lambda functions Algorithms Examples 3 std::copy() / std::copy_if() - Takes two input iterators and an output iterator (and predicate, in case of the _if version) We CAN'T just give it an iterator for the container. It doesn't know how to add new objects there! Two approaches: 1 Prepare the container to have enough size to overwrite with the new copied objects 2 Use an iterator that knows how to insert new objects, e.g. std::back_inserter_iterator
Outline Part II Backlog 3 4 5 6 7 Class Special Member Functions 8 9
Misc typename for template-depended types For default value, use c-tor calling syntax / brace initialization instead of a simple assignment (of 0, for example) for allowing types with explicit c-tors Use of global (not-in-class) template operators Declaration should be in class (as friend), if we want implicit conversion to work (e.g. for operator* for Zp class)
Template specializations Template explicit specialization (full specialization) - relevant for classes and functions (but see below) Template partial specialization - relevant for classes but not for functions For functions, it may seems like a partial specialization, but it's overloading But explicit specialization is not part of the overloading resolution! Better to use only overloading (without specialization) or specialize inside a struct
sf::view, sf::viewport sf::view controlls the displayed part of the window Denes which part of the window coordinate system is displayed sf::viewport controlls which part of the window will be used to show the relevant sf::view Converting mouse click position to window coordinates is a must if you start using views and viewports
Converting values (numbers) to/from text Converting to text is easy: std::to_string() Before 11 we used std::(o)stringstream. It's still useful in some cases Converting text to values: Don't use atoi() or even strtoll()! (or sscanf()) Use the safe alternative: stoi() etc. The second argument, pos, can become handy std::(i)stringstream is more useful in many cases
One of the main concepts of OOP Will be more eective when we'll see polymorphism (which is one of OOP principles) vs. real world categories Pinguin example Rectangle and square example
Polymorphism virtual functions Understand how it works (usually) compile-time dispatch vs. run-time dispatch Possible pitfalls Using dierent arguments (override keyword) Usage in c-tor/d-tor Overriding of non-virtual functions Default arguments Overriding part of an overloading set of virtual functions (using) Virtual d-tor nal keyword (but only for virtual functions) Abstract Base Class principle assignment issue
Class special member functions Class Special Member Functions Destructor Frees the class resources Copy c-tor Controls the creation of a new object from another (e.g. for deep copy) Assignment operator Controls copying one object over an existing one (again, for deep copy and freeing the old resources) Move c-tor Controls the creation of a new object from another, when we can steal resources from the original Move assignment operator Controls the copying one object over an existing one, when we can steal resources from the original Default c-tor Controls the creation of an object when no parameter has given
More about special member functions Class Special Member Functions Explicitly defaulting or deleting special functions Copy/Move sometimes suppressed by RVO/NRVO Rule-of-thumb to identify when move can't happen: if there are two names to access the same object e.g. returning a function static variable
A common misconcept about c-tors Class Special Member Functions Calling a c-tor from within another c-tor body doesn't do what you maybe think it does! It isn't the same as calling a c-tor from another c-tor member initializer list (the term for such a call is delegation)
Compiler errors vs. Linker errors Compiler errors include source line number; Linker errors - only (object) le name Linker errors are usually more complicated and less understandable
2 most common Linker errors Double denition Unresolved externals (unresolved reference)
vs. C Line comments (// vs. block comments - /* comment */) References (so don't use pointers, until we'll have to) const (ported back to C, too) - use it wisely wherever you can; don't use #dene Function overloading Default values for function arguments Namespaces Exceptions OOP (class, public, private, etc.) Generic programming (Templates) Much richer standard library (including STL)
Introduction 11/14 Feels like a new language (Bjarne Stroustrup) We'll see where there are diereneces and what the 98/03 way to do things was VS2015 implements almost all 11/14 and even a bit from 17 Pros and Cons Course objectives