How to use foreach loop equivalent in C++

3 Answers

0 votes
#include <iostream>

int main() 
{ 
    int arr[] = {1, 2, 3, 4, 5, 6};  
    
    for (int n : arr) {
    	std::cout << n << " ";
    }
}



/*
run:
    
1 2 3 4 5 6 	
    
*/

 



answered Jul 12, 2024 by avibootz
0 votes
#include <iostream>
#include <array>

int main() 
{ 
    std::array<std::string, 4> arr = {"c", "c++", "python", "java"};
    
    for (const std::string& str : arr) {
        std::cout << str << " ";
    }
}



/*
run:
    
c c++ python java 	
    
*/

 



answered Jul 12, 2024 by avibootz
0 votes
#include <iostream>
#include <vector>

int main() 
{ 
    std::vector<std::string> vec{"c", "c++", "python", "java", "c#"};
    
    for (auto str: vec) {
        std::cout << str << " ";
    }
}



/*
run:
    
c c++ python java c# 	
    
*/

 



answered Jul 12, 2024 by avibootz

Related questions

1 answer 149 views
149 views asked Mar 6, 2023 by avibootz
1 answer 159 views
159 views asked Mar 30, 2021 by avibootz
1 answer 133 views
1 answer 171 views
171 views asked Dec 2, 2020 by avibootz
2 answers 187 views
187 views asked Nov 25, 2020 by avibootz
1 answer 130 views
130 views asked Aug 7, 2020 by avibootz
1 answer 189 views
...