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 146 views
146 views asked Mar 6, 2023 by avibootz
1 answer 154 views
154 views asked Mar 30, 2021 by avibootz
1 answer 130 views
1 answer 167 views
167 views asked Dec 2, 2020 by avibootz
2 answers 181 views
181 views asked Nov 25, 2020 by avibootz
1 answer 126 views
126 views asked Aug 7, 2020 by avibootz
1 answer 185 views
...