Video Coming Soon...
18: Range for-loops
This exercise is pending. Quick notes about this exercise:
- Demonstrate the problem with range for and temporary results, but might have to just show them the bug so they recognize it, then get into it in detail when we cover functions.
View Source file ex18.cpp Only
#include <fmt/core.h>
#include <vector>
using std::string, std::vector;
using namespace fmt;
/* This is a function, just get this working for now
* and we'll cover them soon.
*/
vector<string> bug_maker() {
return {"Boo! I'm a Bug!"};
}
int main() {
vector<string> fruit = {
"Apple", "Orange", "Pear",
"Grape", "Durian", "Mango"
};
// loop through fruit
for(auto name : fruit) {
println("Fruit is {}", name);
}
// another way to get it using at()
for(auto name : fruit) {
println("Fruit is {}", name);
}
for(auto name : bug_maker()) {
println("Buggy Guitar: {}", name);
}
/*
for(auto name: bug_maker()[0]) {
println("Probably Crashing: {}", name);
}
*/
}
When to Use Which for-loop
Iterators
This might be a good place to cover these since it's related to range for.
std::find
I should include find here as it fits pretty well with range for and iterators.
Register for Learn C++ the Hard Way
Register to gain access to additional videos which demonstrate each exercise. Videos are priced to cover the cost of hosting.