Intermediate C++ Game Programming Tutorial 16

From Chilipedia
Revision as of 05:06, 29 September 2019 by R vdw (Talk | contribs) (Video Timestamp Index)

Jump to: navigation, search

In the video we take a look at a couple of bangers from <algorithm>, and we learn what lambda functions are and how to use them.

Topics Covered

  • Algorithms std::sort and std::remove_if
  • Concepts (as in, type concepts like Comparable)
  • <functional> functors as predicates
  • Lambda functions

Errata

In the video Chili states that std::remove does not preserve the relative order of elements in the container, but this is fake news! Relative order is preserved: [1]

Video Timestamp Index

Tutorial 16

  • Generic Algorithms: how Templates and Iterators make Algorithms independent of Data Types and Containers 0:17
  • The std::sort algorithm and its requirements 0:43
  • Sorting a (forward) list std::list, using its member function std::list::sort() 2:16
  • Introducing Concepts / named requirements of types (e.g., the Compare requirement of std::sort 2:54
  • Modyfing sorting order by passing a comparison function object (functor) as a predicate, like so:
    std::sort(vec.begin(),vec.end(),std::greater<int>{}); 3:17
    Note: vec is a std::vector of integers
  • Sort custom types, example code for sorting a custom class object by creating a functor, called like so:
    std::sort(vecObj.begin(),vecObj.end(),Obj::YLess{}); 4:07
  • Removing items from a container, std::remove and std::remove_if 6:11
    - Create a functor as a unary predicate for the std::remove function to remove elements using a threshold variable 8:28
    - Use container member function erase() to correct the endpoint of the container iterator after the remove operation 9:51
  • Introducing the Lambda function to replace custom functors 11:51
    - Using the Lambda capture to bring in variables into the Lambda function 13:34
    - Capture syntax explained: [varl],[&var],[=],[&],[newvar=var+10],[this], capture multiple variables, mix&match capture modes
  • Stateful Lambdas; changing captured lambda variables using the mutable flag 15:59
  • Some anxiety reducing consolation by Chili 16:40
  • Syntax for a callable Lambda function 17:03
    - Make it callable once: [...](...){...}(vec.front());, functor is destroyed immediately
    - Keep the functor using auto mylamb = [...](...){...};, which can be called by bool result = mylamb(vec.front()), or std::remove_if(...,...,mylamb);
  • The power of C++ is in its flexibility. Example code for converting a string to lowercase:
    std::transform(str.begin(),str.end(),str.begin(),::tolower);17:49
  • Homework assignment 18:39
  • Introducing the [Algorithm Gems] series 19:20

Homework

Solve the problems in Source.cpp attached below using the <algorithm> library and other parts of the standard library as extensively as possible. As a bonus problem, implement the sprite drawing effect shown at the end of the video using a lambda function.

Related Series

For an in-depth look into the <algorithm> library and beyond, check out STD Gems.

Links

Algorithms Library

See also