0

How to do like that in C++?

listInt == [1, 2, 3, 4] for i in listInt: print(i) output: 1 2 3 4

1st Jun 2017, 3:02 AM
School Dog
5 odpowiedzi
+ 4
#include <iostream> int main() { int list[] = { 1, 2, 3, 4 }; for (auto i : list) std::cout << i << "\n"; }
1st Jun 2017, 3:29 AM
aklex
aklex - avatar
+ 2
@Glozi30 The problem is that endl flushes the stream and then starts a new line. This is drastically slower and inefficent compared to \n by itself
1st Jun 2017, 6:23 AM
aklex
aklex - avatar
+ 1
i would just add two things to the aklex answer : * use std::endl instead of "\n" is a good use, because it makes your code easier to read. * don't forget the return 0; at the end of the code, even if the playground on sololearn doesn't seem to need it, some compilers won't compile your code without it. There is also the int main(int argc, char*argv[]) but that is an other history and you don't need it right now.
1st Jun 2017, 5:18 AM
Glozi30
Glozi30 - avatar
0
@aklex "In many implementations, standard output is line-buffered, and writing '\n' causes a flush anyway, unless std::ios::sync_with_stdio(false) was executed" taken from cpp docs ^^
1st Jun 2017, 8:45 AM
Jakub Stasiak
Jakub Stasiak - avatar
- 1
tks
1st Jun 2017, 6:49 AM
School Dog