Difference between revisions of "Intermediate C++ Game Programming Tutorial 15"
(Created page with "In this tutorial we learn how to make our own goddamn templates. So much power. Drunk with it we are. We also learn about the concept of a "functor" (dumb name, clever idea),...") |
|||
Line 1: | Line 1: | ||
− | In this | + | Iterators are just fancy pointers for containers. In this video we learn how to use iterators, what they're good for, and we go through all this trouble in the first place (answer: because they enable some pretty sweet shit). |
== Topics Covered == | == 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 types (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 == | == Video Timestamp Index == | ||
− | [https://youtu.be/1iOgXtAyxz8 Tutorial | + | [https://youtu.be/1iOgXtAyxz8 Tutorial 15] |
− | == | + | == 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 == | == Homework == | ||
− | Modify stack to work with range based for loops. | + | 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. |
+ | |||
+ | [www.github stack] | ||
== Links == | == Links == | ||
[http://www.cplusplus.com/reference/iterator/ Iterator Type Reference] | [http://www.cplusplus.com/reference/iterator/ Iterator Type Reference] | ||
− | |||
− | |||
== See also == | == See also == | ||
− | * [[Intermediate C++ Game Programming Tutorial | + | * [[Intermediate C++ Game Programming Tutorial 16|Next in series (Tutorial 16)]] |
* [[Intermediate C++ Game Programming Series]] | * [[Intermediate C++ Game Programming Series]] |
Revision as of 14:54, 14 October 2017
Iterators are just fancy pointers for containers. In this video we learn how to use iterators, what they're good for, and we go through all this trouble in the first place (answer: because they enable some pretty sweet shit).
Contents
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 types (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
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.
[www.github stack]
Links