Video Coming Soon...

Created by Zed A. Shaw Updated 2024-10-28 08:02:26

18: Range for-loops

This exercise is pending. Quick notes about this exercise:

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.

Previous Lesson Next Lesson

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.