I have 2 loops:
//Loop 1
for ( vector<string>::iterator iter = vecv.begin() ; iter != vecv.end() ; iter++)
{
cout << "-----IN LOOP----" << *iter;
}
//Loop 2
for ( vector<string>::iterator iter = vecv.begin() ; iter != vecv.end() ; iter++)
{
cout << "-----IN LOOP----" << *iter << endl ;
}
Now vecv is a string vector and contains 2 strings:
65 and A000 respectively.
Now the loop 1 Does not print anything, in fact the loop seems to not run at all.
However on adding the endl as you see in Loop 2, it gives this output:
-----IN LOOP----65
-----IN LOOP----$A000
What is happening exactly?
Mind you i am facing this problem only in Visual Studio 2010 and not Dev-C++!!
How do you know it doesn’t print anything without
endl? By observing running program? If so, the problem is thatcoutprovides buffered output: it actually prints somehting on the screen (or in the file) only when its buffer is full.endlon other hands flushes the buffer.If you want unbuffered output you can use
cerr, but it would lead to some differences (like, now if you want to redirect output to file you should do2>instead of>etc.)If you want buffered output but don’t want to have newlines, print
flushintocout.