Daphne Drew
25
Q:

c++ range based for loop

array<int, 5> values = {1, 2, 3, 4, 10};
// the type declaration below must be consistent with the array type
for (int x : values){ //we use a colon instead of in
cout << x << endl;
}
3
// Illustration of range-for loop 
// using CPP code 
#include <iostream> 
#include <vector> 
#include <map> 
  
//Driver 
int main()  
{ 
    // Iterating over whole array 
    std::vector<int> v = {0, 1, 2, 3, 4, 5}; 
    for (auto i : v) 
        std::cout << i << ' '; 
      
    std::cout << '\n'; 
      
    // the initializer may be a braced-init-list 
    for (int n : {0, 1, 2, 3, 4, 5}) 
        std::cout << n << ' '; 
      
    std::cout << '\n'; 
   
    // Iterating over array 
    int a[] = {0, 1, 2, 3, 4, 5};      
    for (int n : a) 
        std::cout << n << ' '; 
      
    std::cout << '\n'; 
      
    // Just running a loop for every array 
    // element 
    for (int n : a)   
        std::cout << "In loop" << ' '; 
      
    std::cout << '\n'; 
      
    // Printing string characters 
    std::string str = "Geeks"; 
    for (char c : str)  
        std::cout << c << ' '; 
          
    std::cout << '\n'; 
  
    // Printing keys and values of a map 
    std::map <int, int> MAP({{1, 1}, {2, 2}, {3, 3}}); 
    for (auto i : MAP) 
        std::cout << '{' << i.first << ", " 
                  << i.second << "}\n"; 
} 
0
1 #include <iostream>
2 #include <array>
3 #include <cstdlib>
4
5 using namespace std;
6
7 int main(){
8 array<int, 5> d = {1, 2, -1, 3, 5};
9 cout << "Items before modification: " << endl;
10 for (int item : d){
11 cout << item << " ";
12 }
13 //multiple elements of d by 3
14 for (int &itemRef : d){
15 itemRef *= 3;
16 }
17 cout << endl << "Items after modification: " << endl;
18 for (int item : d){
19 cout << item << " ";
20 }
21 cout << endl;
22 return 0;
23 }
1
for (<variable_declaration> : expression){
//statements
}
1
for ( range_declaration : range_expression ) 
    loop_statement

Parameters :
range_declaration : 
a declaration of a named variable, whose type is the 
type of the element of the sequence represented by 
range_expression, or a reference to that type.
Often uses the auto specifier for automatic type 
deduction.

range_expression : 
any expression that represents a suitable sequence 
or a braced-init-list.

loop_statement : 
any statement, typically a compound statement, which
is the body of the loop.
0

New to Communities?

Join the community