From 8f137df5891b7a27c3da3703fe031f1cb61867d7 Mon Sep 17 00:00:00 2001 From: NehaKeshan <39170739+NehaKeshan@users.noreply.github.com> Date: Mon, 2 Oct 2023 19:46:18 -0400 Subject: [PATCH] Update README.md styling fixes for additional iterator operations --- lectures/10_linked_lists/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lectures/10_linked_lists/README.md b/lectures/10_linked_lists/README.md index 1de2050..60fc097 100644 --- a/lectures/10_linked_lists/README.md +++ b/lectures/10_linked_lists/README.md @@ -106,15 +106,23 @@ cout << a[i] << endl; ```cpp std::list lst; lst.push_back(100); lst.push_back(200); lst.push_back(300); lst.push_back(400); lst.push_back(500); +``` + +```cpp std::list::iterator itr,itr2,itr3; itr = lst.begin();// itr is pointing at the 100 ++itr; // itr is now pointing at 200 *itr += 1; // 200 becomes 201 // itr += 1; // does not compile! can't advance list iterator like this +``` +```cpp itr = lst.end(); // itr is pointing "one past the last legal value" of lst itr--; // itr is now pointing at 500; itr2 = itr--; // itr is now pointing at 400, itr2 is still pointing at 500 itr3 = --itr; // itr is now pointing at 300, itr3 is also pointing at 300 +``` + +```cpp // dangerous: decrementing the begin iterator is "undefined behavior" // (similarly, incrementing the end iterator is also undefined) // it may seem to work, but break later on this machine or on another machine!