From ba43fdc3dc83c6cec44313b86da3702139b68f61 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 9 Feb 2024 16:16:34 -0500 Subject: [PATCH] get rid of the keyword auto --- lectures/09_iterators_linked_lists/README.md | 12 ++++++++---- .../09_iterators_linked_lists/list_sort_objects.cpp | 13 ++++++++----- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/lectures/09_iterators_linked_lists/README.md b/lectures/09_iterators_linked_lists/README.md index 4b293e6..2755f3e 100644 --- a/lectures/09_iterators_linked_lists/README.md +++ b/lectures/09_iterators_linked_lists/README.md @@ -185,8 +185,10 @@ int main() { // Print the original list std::cout << "Original list:" << std::endl; - for (const auto& person : people) { - std::cout << person.name << " (" << person.age << ")" << std::endl; + std::list::iterator itr = people.begin(); + while(itr != people.end()){ + std::cout << (*itr).name << " (" << (*itr).age << ")" << std::endl; + ++itr; } // Sort the list of Person objects using the custom comparison function @@ -194,8 +196,10 @@ int main() { // Print the sorted list std::cout << "\nSorted list:" << std::endl; - for (const auto& person : people) { - std::cout << person.name << " (" << person.age << ")" << std::endl; + itr = people.begin(); + while(itr != people.end()){ + std::cout << (*itr).name << " (" << (*itr).age << ")" << std::endl; + ++itr; } return 0; diff --git a/lectures/09_iterators_linked_lists/list_sort_objects.cpp b/lectures/09_iterators_linked_lists/list_sort_objects.cpp index 77ba4f3..23739ee 100644 --- a/lectures/09_iterators_linked_lists/list_sort_objects.cpp +++ b/lectures/09_iterators_linked_lists/list_sort_objects.cpp @@ -27,8 +27,10 @@ int main() { // Print the original list std::cout << "Original list:" << std::endl; - for (const auto& person : people) { - std::cout << person.name << " (" << person.age << ")" << std::endl; + std::list::iterator itr = people.begin(); + while(itr != people.end()){ + std::cout << (*itr).name << " (" << (*itr).age << ")" << std::endl; + ++itr; } // Sort the list of Person objects using the custom comparison function @@ -36,10 +38,11 @@ int main() { // Print the sorted list std::cout << "\nSorted list:" << std::endl; - for (const auto& person : people) { - std::cout << person.name << " (" << person.age << ")" << std::endl; + itr = people.begin(); + while(itr != people.end()){ + std::cout << (*itr).name << " (" << (*itr).age << ")" << std::endl; + ++itr; } return 0; } -