3 ms·
With C++11, it's: #include <cstdio> int main(void) { const int xs[] = {1, 2, 3, 4, 5, 6, 7}; for (int i : xs) if (i < 5) printf("%d\n", i);
by ksherlock 11y ago
With C++11, it's:
#include <cstdio>
int main(void) {
const int xs[] = {1, 2, 3, 4, 5, 6, 7};
for (int i : xs) if (i < 5) printf("%d\n", i);
return 0;
}
Maybe I cheated because I use stdio and didn't waste 15 minutes trying to make a useless copy of the array.
- Chris_Newton 11y agoIf you look at it that way, we might as well write: #include <iostream> int main() { std::cout << "1\n2\n3\n4\n"; } However, the point of the discussion was the ease or otherwise of performing some basic data manipulation depending on the syntax of the programming language used. Of course we can skip that data manipulation and go direct to the result in this trivial example, but that doesn’t illustrate anything very useful about programming with the different syntactic styles more generally.