C++ Reference ยท Strings and I/O
C++ iostream library overview
Overview of the C++ iostream library: the stream class hierarchy, cin, cout, cerr and clog, operators << and >>, buffering, sync_with_stdio and tie.
The iostream library is C++'s classic input and output framework. Instead of format strings it uses streams: objects that you write to with << and read from with >>. A stream hides where the characters go, so the same code can talk to the console, a file or a string in memory.
The header <iostream> declares four global stream objects: std::cin for standard input, std::cout for standard output, std::cerr for unbuffered error output and std::clog for buffered log output. Each also has a wide-character twin (wcin, wcout, wcerr, wclog).
#include <iostream> #include <string> int main() { std::cout << "Your name? "; std::string name; std::cin >> name; std::cout << "Hello, " << name << "\n"; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
The stream hierarchy
std::ios_baseholds formatting flags, precision and width.std::basic_iosadds the stream state (good,fail,eof,bad) and the connection to a buffer (rdbuf).std::istreamadds input (>>,get,getline,read,peek,ignore);std::ostreamadds output (<<,put,write,flush);std::iostreamhas both.std::ifstream,std::ofstream,std::fstreamattach a file;std::istringstream,std::ostringstream,std::stringstreamattach a string. See file streams and string streams.- Forward declarations of all of these are in
<iosfwd>;<istream>and<ostream>define the classes without the global objects.
The four standard streams
| Object | Direction | Buffering | Typical use |
|---|---|---|---|
std::cin |
input | buffered | reading user input, see std::cin |
std::cout |
output | buffered | normal program output, see std::cout |
std::cerr |
output (stderr) | unbuffered | error messages; flushes cout first because they are tied |
std::clog |
output (stderr) | buffered | diagnostic logging |
Usage notes
- Operators chain because
<<and>>return the stream.cout << a << bis two calls. - Failures are silent. A bad read sets
failbitand later reads do nothing until you clear it. Always test the stream (if (cin >> x)). See stream states. '\n'versusstd::endl.endlwrites a newline and flushes. Flushing on every line is slow in loops, so prefer'\n'and flush only when needed.std::ios::sync_with_stdio(false)stops iostream from staying in lockstep with C'sstdio. It speeds up heavy input and output but you must not mixcin/coutwithscanf/printfafterwards. Call it before any I/O.cin.tie(nullptr)removes the automatic flush ofcoutbefore each input operation. Together with the line above it is the usual competitive-programming speed setup, but interactive prompts then may not appear before the program waits.- Locale. Streams use a locale for numbers; the default is the classic "C" locale, which prints
1234.5and not1.234,5. - Prefer
<print>for new output code.std::printlnis type safe like streams, but formats a whole line in one call and has no sticky state. Streams remain the way to read input and the base for file and string I/O. - Customize. Give your class
std::ostream& operator<<(std::ostream&, const T&)to print it with<<.
Related
std::cout,std::cin,std::getline, Stream states,std::fstream- Lessons: iostreams, Output, User input