diff --git a/debugging/src/buggy.cpp b/debugging/src/buggy.cpp index ba52144..7afa088 100644 --- a/debugging/src/buggy.cpp +++ b/debugging/src/buggy.cpp @@ -21,18 +21,18 @@ class Student { Student* getStudent(){ Student* myStudent = new Student(10801983,"Mary Wright"); -// ***** OPS! - return nullptr; + return myStudent; } void printStudent(Student* myStudent){ std::cout << "-- Student's Data --\n"; - std::cout << "Student's ID is " << myStudent->getName() - << " and name is " << myStudent->getID() << std::cout; + std::cout << "Student's ID is " << myStudent->getID() + << " and name is " << myStudent->getName() << std::endl; } int main(){ Student* myStudent = getStudent(); printStudent(myStudent); + delete myStudent; return 1; } diff --git a/modernsyntax/classical_looping.cpp b/modernsyntax/classical_looping.cpp index 6b0b0d5..db0d1d4 100644 --- a/modernsyntax/classical_looping.cpp +++ b/modernsyntax/classical_looping.cpp @@ -28,7 +28,17 @@ void indexLoop(std::vector& tools){ } void iteratorLoop(std::vector& tools){ - for(std::vector::const_iterator tool = tools.begin(), end = tools.end(); tool != end; ++tool){ + for(auto tool = tools.begin(), end = tools.end(); tool != end; ++tool){ + tool->print(); + } +} + +void rangeLoop(std::vector& tools, const int beginning, const int ending){ + auto tool = tools.begin(), end = tools.end(); + std::advance(tool,beginning); + auto stop_point = std::distance(tools.begin(), end) - ending; + + for( ; std::distance(tool, end) > stop_point; ++tool){ tool->print(); } } @@ -42,6 +52,8 @@ int main(){ indexLoop(tools); std::cout << "Start iterator looping" << std::endl; iteratorLoop(tools); + std::cout << "Start range looping" << std::endl; + rangeLoop(tools, 2, 4); return 0; } diff --git a/modernsyntax/lambda.cpp b/modernsyntax/lambda.cpp index da44e68..abdff60 100644 --- a/modernsyntax/lambda.cpp +++ b/modernsyntax/lambda.cpp @@ -13,7 +13,9 @@ void incrementCounter(int* counter, const unsigned int times){ int main(){ int counter(0); // launch a thread to increment the counter - std::thread increment(incrementCounter,&counter,100000); + + const unsigned int times = 100000; + std::thread increment([×, &counter](){for (unsigned int i=0; i tools){ - for(auto tool : tools){ +void rangeLoop(std::vector& tools){ + for(auto&& tool : tools){ tool.print(); } }