In C++, the for
loops are one of the great features of C and C++ language that has many options to break, continue, or iterate in a loop. In modern C++, there is a range-based for loop that makes it simple to iterate through a variable type that has members (i.e. strings, lists, arrays, vectors, maps, etc.). The range-based for loop is a feature for the for() loops introduced by the C++11 standard and in this post, we explain what is range-based for loop in examples.
If you are new to programming and looking for a classic for() loop here is the post about it.
Table of Contents
What is range-based for loop in modern C++?
In C++, for()
function is used for loops, and they are one of the great features of C and C++ language and have many options to break or continue or iterate blocks of functionality. In modern C++, there is a range-based for loop that makes it simple to iterate trough a variable type that has members (i.e. strings, lists, arrays, vectors, maps, etc.).
The range-based for loop is a feature for the for() loops introduced by the C++11 standard. Range-based for loop is a feature for the for()
loops introduced by the C++11 standard. In Clang-enhanced C++ compilers, you can create for
loops that iterate through a list or an array without computing the beginning, the end, or using an iterator.
Here is the syntax for the range-based for loop in C++:
1 2 3 |
attr (optional) for ( init_statement (optional) range_declaration : range_expr) loop_statement; |
or with loop body:
1 2 3 4 5 6 |
attr (optional) for ( init_statement (optional) range_declaration : range_expr) { // loop_statements } |
Is there a simple array example with a range-based for loop in C++?
We can use range-based for loop to iterate through an array
as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> int main() { int arr[] = { 00, 10, 20, 30, 40, 50 }; for (int a : arr) std::cout << a << ','; system("pause"); return 0; } |
Is there a string example with range-based for loop in C++?
The following example shows how to use range-based for loop to iterate on the characters of a string
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> int main() { std::string str("LearnCPlusPlus.org"); for (char& c: str) { std::cout << c << '.'; } system("pause"); return 0; } |
here, we can use iterators as below,
1 2 3 4 5 6 |
for(std::string::iterator it = str.begin(); it != str.end(); ++it) { std::cout << it << '.'; } |
or we can use as in classic for-loop as below,
1 2 3 4 5 6 |
for(std::string::size_type i = 0; i < str.size(); ++i) { std::cout << str[i] << '.'; } |
or it can be used through the characters of a null-terminated character array,
1 2 3 4 5 6 7 8 |
char s[] = "LearnCPlusPlus.org"; char *c = s; for(char* it = s; *it; ++it) { std::cout << *it << '.'; } |
Is there a UnicodeString example with range-based for loop in C++?
We can use range-based for loop to iterate UnicodeString
or String
in C++ Builder VCL or FMX applications as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner) { UnicodeString ustr("LearnCPlusPlus.org"); UnicodeString ustr2(""); for (auto& character : ustr) { ustr2+= (String)character + "."; } ShowMessage(ustr2); } |
Note that, in the newer version all String
definitions are UnicodeString
, that means you can use in String
too.
Is there a vector example with a range-based for loop in C++?
Mostly range-based for loops are used to iterate through an vectors
as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <vector> int main() { std::vector<int> vec = { 00, 10, 20, 30, 40, 50 }; for (auto v : vec) std::cout << v << '.'; system("pause"); return 0; } |
Is there a map example with a range-based for loop in C++?
Mostly range-based for loops are used to iterate through an vectors
as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <map> int main() { std::map<int, int> mymap( { { 0, 10 }, { 1, 20 }, { 2, 30 } }); for (auto m : mymap) { std::cout << m.first << "," << m.second << std::endl; } system("pause"); return 0; } |
The range-based for loop is really useful to use in all ranges of data containers.
For more information on this feature and more details about ranges, see Ranges for the Standard Library.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition