From cb7ea6977f837fc7e85672280dbf57cd099ab652 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 9 Feb 2024 13:38:32 -0500 Subject: [PATCH] adding the iterator exercise code --- lectures/10_linked_lists/README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lectures/10_linked_lists/README.md b/lectures/10_linked_lists/README.md index 614b48b..6bdb959 100644 --- a/lectures/10_linked_lists/README.md +++ b/lectures/10_linked_lists/README.md @@ -140,6 +140,36 @@ itr++; assert (*itr == 100); // might seem ok... but rewrite the code to avoid this! ``` +### Exercise + +What is the output of this program? + +#include +#include + +```cpp +int main(){ + + std::list lst; + lst.push_back(150); + lst.push_back(250); + lst.push_back(350); + lst.push_back(450); + + std::list::iterator itr; + itr = lst.begin(); + ++itr; + *itr += 5; + + std::list::iterator itr2 = lst.begin(); + while(itr2 != lst.end()){ + std::cout << *itr2 << std::endl; + itr2++; + } + +} +``` + ## 10.3 Working towards our own version of the STL list - Our discussion of how the STL list<T> is implemented has been intuitive: it is a “chain” of objects.