2) I recommend switching i++ with ++i. The pre-fix increment/decrement is more efficient because post-fix increment/decrement pushes an extra int onto the stack and returns a copy of the original operand.
3) Use std::endl() less. Excessive calls to std::endl() will hinder performance. Replace each occurrence with '\n' and before the end of the program, flush the stream with std::cout.flush().
@Framework
Are you serious? The program is clearly I/O bound, saving a few processor ticks won't change anything. If we are talking about performance, I think using C stdout could help.
As for the original question - obviously you should use nested loops.
Judging by the title, he/she wanted efficiency, so I gave tips on how the code could be improved, so stop moaning. By the way, how is the file system faster than memory? How could that improve efficiency?
I was not moaning. I was simply saying that the "tricks" you presented could be potentialy of any use for a person who wants to optimize a CPU bottleneck in his program. Trying to use them in any other situation is counterproductive and a waste of time. Besides, as others mentioned, the original question is rather about writing succinct code.
By saying stdout, I meant using printf rather than cout. This could actually improve efficiency, unlike your suggestions (except for 3rd).