C++ cout printing slowly

NOTE: This experimental result is valid for MSVC. In some other implementation of library, the result will vary. printf could be (much) faster than cout. Although printf parses the format string in runtime, it requires much less function calls and actually needs small number of instruction to do a same job, comparing to cout. Here … Read more

What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?

Generally you use std::cout for normal output, std::cerr for errors, and std::clog for “logging” (which can mean whatever you want it to mean). The major difference is that std::cerr is not buffered like the other two. In relation to the old C stdout and stderr, std::cout corresponds to stdout, while std::cerr and std::clog both corresponds … Read more

When does cout flush?

There is no strict rule by the standard – only that endl WILL flush, but the implementation may flush at any time it “likes”. And of course, the sum of all digits in under 400K is 6 * 400K = 2.4MB, and that’s very unlikely to fit in the buffer, and the loop is fast … Read more

How to print to console when using Qt

If it is good enough to print to stderr, you can use the following streams originally intended for debugging: #include<QDebug> //qInfo is qt5.5+ only. qInfo() << “C++ Style Info Message”; qInfo( “C Style Info Message” ); qDebug() << “C++ Style Debug Message”; qDebug( “C Style Debug Message” ); qWarning() << “C++ Style Warning Message”; qWarning( … Read more

Hide user input on password prompt [duplicate]

From How to Hide Text: Windows #include <iostream> #include <string> #include <windows.h> using namespace std; int main() { HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); DWORD mode = 0; GetConsoleMode(hStdin, &mode); SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT)); string s; getline(cin, s); cout << s << endl; return 0; }//main cleanup: SetConsoleMode(hStdin, mode); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); Linux #include <iostream> #include <string> … Read more