Raw for loops are often the cause of subtle and not so subtle bugs. Troy shows how range-based for loops prevent most of these bugs.
- [Instructor] C plus plus can iterate…over a range of values automatically.…No need for indexing.…It can iterate over STL containers, strings, and arrays.…You can add support for range-based for loops…to your own types by supporting the iterator interface.…Prefer range-based for loops over traditional for loops,…but always ask yourself twice before implementing…any raw loop if you can replace it…with an algorithm from the STL.…Let's see some code examples.…
So in our first example, we're just iterating over a vector…of int, and you notice the format is just four,…and then we got auto I colon, and then…the vector of int which is v.…Then we have it iterating over a initialized list,…and so we don't even have to declare it as a variable.…We can just put the raw list right inside the for loop,…and then we have it iterating over just standard arrays.…It can even iterate over strings,…and we can iterate over map.…
So let's just take a quick look at it in code.…Let's go ahead and compile this.…So we're going to do a c lang plus plus…
- Major new C++ features
- Turning on modern C++
- Letting the compiler help
- Using the new features correctly
- Using auto type deduction
- Using lambdas
- Range-based for loops
- How C++ and the Standard Template Library name things
- Making the best of the filesystem library
- Managing objects
Skill Level Beginner
1. What Is Modern C++?
2. Tools That Improve Your Code
3. Using the New Features Correctly
4. Don't Reinvent the Wheel
5. Managing Objects
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.