Difference between revisions of "Intermediate C++ Game Programming Tutorial 15"

From Chilipedia
Jump to: navigation, search
(Video Timestamp Index)
(Video Timestamp Index)
Line 26: Line 26:
 
* Why not just use indices? Iterators and template algorithms (generic algorithms) [https://youtu.be/h5aFJJp1Stw?t=12m36s 12:36]
 
* Why not just use indices? Iterators and template algorithms (generic algorithms) [https://youtu.be/h5aFJJp1Stw?t=12m36s 12:36]
 
* Introducing the <code>forward_list</code> container [https://youtu.be/h5aFJJp1Stw?t=13m40s 13:40]
 
* Introducing the <code>forward_list</code> container [https://youtu.be/h5aFJJp1Stw?t=13m40s 13:40]
* Template a fuction on Iterator: writing a generic function <code>template<typename Iter></code>, <code>void print(Iter begin, Iter end)</code> [https://youtu.be/h5aFJJp1Stw?t=14m35s 14:35]
+
* Template a fuction on Iterator: writing a generic function that can be applied to any type of container <code>template<typename Iter></code>, <code>void print(Iter begin, Iter end)</code> [https://youtu.be/h5aFJJp1Stw?t=14m35s 14:35]
 
* (WORK IN PROGRESS)
 
* (WORK IN PROGRESS)
  

Revision as of 03:59, 23 September 2019

Iterators are just fancy pointers for containers. In this video we learn how to use iterators, what they're good for, and why we go through all this goddamn trouble in the first place (answer: because they make my dick hard).

Topics Covered

  • Iterator operations (increment/decrement, dereference, add/sub, etc.)
  • Getting iterators from containers (begin/end)
  • Sequence ranges [closed,open)
  • Container manipulation functions (erase/insert)
  • Iterators and template algorithms (generic algorithms)
  • Iterator categories (random access/bidirectional/forward etc.)
  • const_iterator (cbegin/cend) & reverse iterators (rbegin/rend)
  • Free iterator functions (std::begin/end/advance/next/prev)
  • Iterator adapters (back_inserter, ostream_iterator)

Video Timestamp Index

Tutorial 15

  • Why should I care about iterators? 0:21
  • What are iterators? 0:49
  • Valid range for an iterator [first,last) 1:27
  • What is the type of an iterator / iterator member type definitions 1:55
  • Getting an iterator from a container, using example std::vector<int>::iterator i = v.begin(); 2:34
  • Simple for loops with iterators & iterator arithmetic 3:52
  • Iterator protection and code safety when misused / out of bounds / invalidated 6:48
  • Container manipulation function erase 7:20
  • Iterators that return iterators (recovering from an invalidation) 10:17
  • Container manipulation function insert / emplace 11:45
  • Why not just use indices? Iterators and template algorithms (generic algorithms) 12:36
  • Introducing the forward_list container 13:40
  • Template a fuction on Iterator: writing a generic function that can be applied to any type of container template<typename Iter>, void print(Iter begin, Iter end) 14:35
  • (WORK IN PROGRESS)

Answer to erase puzzle

The reason why we do not increment the iterator i in the for loop, but instead increment it conditionally in the body of the loop is, if we erase an element, the returned iterator will point to the next element after the erased one, and if we were incrementing every iteration of the loop, it would skip all elements that immediately follow an erased element. Instead, we only increment if we did not erase an element, and this ensures that all elements are tested.

Homework

Modify our linked list stack to work with range based for loops. You are not allowed to modify any code outsize of the Stack class. If you want an extra challenge after completing Level 1, you can uncomment the code for Level 2 and try to get it to compile as well.

Linked List Stack Repo

Solution video

Links

Iterator Category Reference
Custom Iterator Example

See also