for loop

1
2
for(char& c: input)


How does this work?
So it would be equal to
 
 for(int i = 0; i <= input.length; i++)
"For every char in 'input'...."
So it would be equal to
for(int i = 0; i <= input.length; i++)

You seem to have an indexing error in that.

But close,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for ( char & c : input ) {
  // code
}

// is similar to
for ( size_t pos = 0; pos < count_of_elements_in_input; ++pos ) {
  char & c = input[pos];
  // code
}


// there is also
std::for_each( std::begin(input), std::end(input), []( char & c ) {
    // code
  });
// the std::for_each predates C++11, unlike the std::begin, std::end, and lambda syntax 
Topic archived. No new replies allowed.