I can not understand what the problem is. This code is simply not compiled:
int main(int argc, char * argv[]) { int arr[] = { 1,2,3,4,5,6,7 }; for each (int var in arr) { } system("pause"); } Compile error: expected a { .
What does he lack here?
I can not understand what the problem is. This code is simply not compiled:
int main(int argc, char * argv[]) { int arr[] = { 1,2,3,4,5,6,7 }; for each (int var in arr) { } system("pause"); } Compile error: expected a { .
What does he lack here?
Apparently this is a nonstandard syntax for each, in , implemented at one time in VC ++ and VC ++ / CLI. However, the standard C ++ got an alternative syntax, which should be used:
for(auto const & int: arr) { } in c ++ for each is not a language construct, but a function from the standard STL library, which should be used as follows in your example:
#include <iostream> #include <algorithm> int main(int argc, char * argv[]) { int arr[] = { 1,2,3,4,5,6,7 }; std::for_each(arr, arr + sizeof(arr)/sizeof(arr[0]), [](int el) { std::cout << el << ' '; }); system("pause"); return 0; } for_each algorithm, but the point here is that it is a loop. I read an article about the foric cycle, and wanted to try a couple of examples, but as an author, it simply does not work. - ravigastd::for_each . - ampawdfor (auto el: arr) { ... } - ampawdYour compiler may not support the above version. And rightly so, in general, because it is a complete non-standard.
The standard entry variant (starting with C ++ 11) is:
int main(int argc, char * argv[]) { int arr[] = { 1,2,3,4,5,6,7 }; for (int var : arr) { } } Source: https://ru.stackoverflow.com/questions/870537/
All Articles