C++11 does. They are called range-based fors. Remember that you should qualify the type as a reference or a reference to const.

The workaround for C++03 is BOOST_FOR_EACH or boost::bind in combination with std::for_each. More fancy things are possible with Boost.Lambda. Should you be in the mood to frustrate either yourself or your co-workers I recommend the deprecated binders std::bind1st and std::bind2nd.

Here is some example code:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <boost/lambda/lambda.hpp>
#include <functional>    

int main()
{
  int i = 0;
  std::vector<int> v;
  std::generate_n(std::back_inserter(v), 10, [&]() {return i++;});

  // range-based for
  // keep it simple
  for(auto a : v)
    std::cout << a << " ";
  std::cout << std::endl;

  // lambda
  // i don't like loops
  std::for_each(v.begin(), v.end(), [](int x) { 
      std::cout << x << " ";
    });
  std::cout << std::endl;

  // hardcore
  // i know my lib
  std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
  std::cout << std::endl;


  // boost lambda
  // this is what google came up with
  // using for the placeholder, otherwise this looks weird
  using namespace boost::lambda;
  std::for_each(v.begin(), v.end(), std::cout << _1 << " ");
  std::cout << std::endl;

  // fold
  // i want to be a haskell programmer
  std::accumulate(v.begin(), v.end(), std::ref(std::cout), 
                  [](std::ostream& o, int i) -> std::ostream& { return o << i << " "; });

  return 0;
}
Answer from pmr on Stack Overflow
Top answer
1 of 7
77

C++11 does. They are called range-based fors. Remember that you should qualify the type as a reference or a reference to const.

The workaround for C++03 is BOOST_FOR_EACH or boost::bind in combination with std::for_each. More fancy things are possible with Boost.Lambda. Should you be in the mood to frustrate either yourself or your co-workers I recommend the deprecated binders std::bind1st and std::bind2nd.

Here is some example code:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <boost/lambda/lambda.hpp>
#include <functional>    

int main()
{
  int i = 0;
  std::vector<int> v;
  std::generate_n(std::back_inserter(v), 10, [&]() {return i++;});

  // range-based for
  // keep it simple
  for(auto a : v)
    std::cout << a << " ";
  std::cout << std::endl;

  // lambda
  // i don't like loops
  std::for_each(v.begin(), v.end(), [](int x) { 
      std::cout << x << " ";
    });
  std::cout << std::endl;

  // hardcore
  // i know my lib
  std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
  std::cout << std::endl;


  // boost lambda
  // this is what google came up with
  // using for the placeholder, otherwise this looks weird
  using namespace boost::lambda;
  std::for_each(v.begin(), v.end(), std::cout << _1 << " ");
  std::cout << std::endl;

  // fold
  // i want to be a haskell programmer
  std::accumulate(v.begin(), v.end(), std::ref(std::cout), 
                  [](std::ostream& o, int i) -> std::ostream& { return o << i << " "; });

  return 0;
}
2 of 7
72

In C++11, if your compiler supports it, yes it is. It's called range-based for.

std::vector<int> v;

// fill vector

for (const int& i : v) { std::cout << i << "\n"; }

It works for C style arrays and any type that has functions begin() and end() that return iterators. Example:

class test {
    int* array;
    size_t size;
public:
    test(size_t n) : array(new int[n]), size(n)
    {
        for (int i = 0; i < n; i++) { array[i] = i; }
    }
    ~test() { delete [] array; }
    int* begin() { return array; }
    int* end() { return array + size; }
};

int main()
{
    test T(10);
    for (auto& i : T) {
        std::cout << i;   // prints 0123456789
    }
}
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit6-Arrays › topic-6-3-arrays-with-foreach.html
6.3. Enhanced For-Loop (For-Each) for Arrays — CSAwesome v1
Use the enhanced for each loop with arrays whenever you can, because it cuts down on errors. You can use it whenever you need to loop through all the elements of an array and don’t need to know their index and don’t need to change their values. It starts with the first item in the array ...
🌐
Sololearn
sololearn.com › en › Discuss › 329399 › enhanced-for-loop-vs-for-loop
Enhanced for loop vs. for loop | Sololearn: Learn to code for FREE!
One place where the enhanced for loop is faster than a naively implemented traditional loop is something like this: LinkedList<Object> list = ...; // Loop 1: int size = list.size(); for (int i = 0; i<size; i++) { Object o = list.get(i); /// do stuff } // Loop 2: for (Object o : list) { // do stuff } // Loop 3: Iterator<Object> it = list.iterator(); while (it.hasNext()) { Object o = it.next(); // do stuff } In this case Loop 1 will be slower than both Loop 2 and Loop 3, because it will have to (partially) traverse the list in each iteration to find the element at position i.
🌐
Sololearn
sololearn.com › en › Discuss › 16221 › what-is-difference-between-normal-for-loop-and-enhanced-for-loop-
What is difference between Normal For loop and Enhanced For Loop ? | Sololearn: Learn to code for FREE!
it took less memory in stack,i.e less frames will be there in stack,efficiency of the code will be high| ... in enhanced for loop ,"we access all the elements of array" and for this purpose only this enhanced for loop is used. we can do above work by using normal for loop also.but coding is ...
🌐
Jake Varness
jvarness.blog › home › my gripes about c-style for-loops
My Gripes about C-Style for-loops | Jake Varness
January 22, 2026 - This addresses all of the concerns we had about C-Style for-loops in terms of stability. Enhanced for-loops can be used with any data structure that implements Java’s Iterable interface, and because the Collection interface extends Iterable, we can use it for every List and Set in our code.
🌐
Quora
quora.com › What-is-the-purpose-of-using-an-enhanced-for-loop-instead-of-a-regular-for-loop-in-Java
What is the purpose of using an enhanced for loop instead of a regular for loop in Java? - Quora
Answer (1 of 2): The purpose of the enhanced for loop is to make code clearer by eliminating a variable whose only purpose is to act as a counter. Although the C-style for loop can have very complex expressions, 99% of all for loops just look ...
🌐
Quora
quora.com › Is-enhanced-for-loop-faster
Is enhanced for loop faster? - Quora
Answer (1 of 2): Is enhanced for loop faster? Only in Java (maybe). You try to use it in C or C++, any speedups are countered by the tie lost fixing your compile errors. And you forgot to add “faster than (the other choice)” to your question - there will be times when enhanced for loop ...
🌐
CodeHS
codehs.com › textbook › apcsa_textbook › 6.3
6.3 Enhanced for Loop for Arrays
5. Writing Classes · 6. Arrays · 6.1 Array · 6.2 Traversing Arrays · 6.3 Enhanced for Loop for Arrays · Enhanced for Loops · Enhanced For Loop Applications · Enhanced For Loop · Classroom Array · Updating Values in a Loop · Check Your Understanding ·
Find elsewhere
🌐
Programiz
programiz.com › c-programming › c-for-loop
C for Loop (With Examples)
In programming, loops are used to repeat a block of code. In this tutorial, you will learn to create for loop in C programming with the help of examples.
🌐
Cplusplus
cplusplus.com › forum › beginner › 136366
Enhanced for loop - C++ Forum
June 23, 2014 - [This question is more about java than C++, but thanks to this last one I began to think more deeply] Is that possible to change the values of array using an enhanced for loop in java, as we can do in C++ using a reference variable to iterate through the array?
🌐
Sololearn
sololearn.com › en › Discuss › 2784035 › enhanced-for-loop-geometry-code
Enhanced for Loop, Geometry Code | Sololearn: Learn to code for FREE!
Atul You can work with for each loop here but there is a collection with empty values so doesn't make sense. There is only one input on which you have to take multiple input from user.
🌐
W3Schools
w3schools.com › c › c_for_loop.php
C For Loop
When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:
🌐
Cppreference
en.cppreference.com › w › cpp › language › range-for.html
Range-based for loop (since C++11) - cppreference.com
If the current iteration needs to be terminated within statement, a continue statement can be used as shortcut. If a name introduced in init-statement is redeclared in the outermost block of statement, the program is ill-formed: for (int i : {1, 2, 3}) int i = 1; // error: redeclaration · If range-initializer returns a temporary, its lifetime is extended until the end of the loop, as indicated by binding to the forwarding reference /* range */.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-for-loop
C for Loop - GeeksforGeeks
Step 1: Initialization is the basic step of for loop this step occurs only once during the start of the loop. During Initialization, variables are declared, or already existing variables are assigned some value. Step 2: During the Second Step condition statements are checked and only if the condition is the satisfied loop we can further process otherwise loop is broken.
Published   October 8, 2025
🌐
Sololearn
sololearn.com › en › Discuss › 1921963 › what-is-enhanced-for-loop-and-how-it-works
what is enhanced for loop and how it works? | Sololearn: Learn to code for FREE!
It will index a variable through an array and run the loop content for each element in the array. int[] arr = {5, 3, 8, 6, 0}; for (int el: arr) { //... } //corresponds: for (int i = 0; i < arr.length; ++i) { int el = arr[i]; //... } keyword ...
🌐
GitHub
gist.github.com › maxgoren › a1097616f9f3003e0373801b3728a061
C++ enhanced for loop · GitHub
C++ enhanced for loop. GitHub Gist: instantly share code, notes, and snippets.
🌐
JetBrains
jetbrains.com › help › inspectopedia › ForLoopReplaceableByForEach.html
'for' loop can be replaced with enhanced for loop | Inspectopedia Documentation
December 3, 2025 - Reports for loops that iterate over collections or arrays, and can be automatically replaced with an enhanced for loop (foreach iteration syntax).