Streams are like flexible flow-through containers.
#include <iostream> int main() { const bool b = true; const char c = 'c'; const int i = 1; const double d = 1.1; std::cout << "bool b: " << b << '\n' << "char c: " << c << '\n' << "integer i: " << i << '\n' << "double d: " << d << std::endl; }
Screen output:
Examples of global streams are:
- std::cout (an std::ostream)
- std::cin (an std::istream)
- std::clog (an std::ostream)
- std::cerr (an std::ostream)
Types of streams are:
- std::stringstream: a std::string stream
- std::ostream: output stream
- std::istream: input stream
- std::fstream: file stream
stream operations
Things one can do on a stream:
Stream format flags
A stream format flag is something that can be on or off. Stream format flags are:
- std::ios::skipws: skip whitespace (on by default for input streams)
- std::ios::showbase: indicate the numeric base
- std::ios::showpoint: show decimal point and trailing zeroes for doubles
- std::ios::uppercase: show uppercase characters in hex (ABCDEF) and scientific (E) notation
- std::ios::showpos
- std::ios::unitbuf
Turning a flag on and off is shown in the code below.
#include <iostream> int main() { std::cout << 1 << '\n'; std::cout.setf(std::ios::showpos); std::cout << 1 << '\n'; std::cout.unsetf(std::ios::showpos); std::cout << 1 << '\n'; }
Stream flag field
A stream flag field is a group of options of which only one can be in effect, similar to a radiogroup control. To set a certain option, one needs to clear the flag group field, then set the desired option.
Stream format flags are:
- std::ios::basefield: std::ios::dec, std::ios::hex, std::ios::oct
- std::ios::floatfield: std::ios::fixed, std::ios::scientific
- std::ios::adjustfield: std::ios::left, std::ios::right, std::ios::internal
#include <iostream> int main() { //Turns on decimal notation, //clears current notation (i.e. basefield) std::cout.setf(std::ios::dec,std::ios::basefield); std::cout << 15 << '\n'; //Turns on hexadecimal notation, //clears current notation (i.e. basefield) std::cout.setf(std::ios::hex,std::ios::basefield); std::cout << 15 << '\n'; //Turns on octal notation, //clears current notation (i.e. basefield) std::cout.setf(std::ios::oct,std::ios::basefield); std::cout << 15 << '\n'; }
Stream manipulators
Stream manipulators are like stream formatting flags and formatting fields, except that manipulators are the streamable version of flags and fields.
Stream manipulators are:
- nl
- std::fixed
- std::internal
- std::left
- std::noshowbase
- std::noshowpoint
- std::noshowpos
- std::noskipws
- std::nouppercase
- std::resetiosflags
- std::right
- std::scientific
- std::setbase
- std::setfill
- std::setiosflags
- std::setprecision
- std::setw
- std::showbase
- std::showpoint
- std::showpos
- std::skipws
- std::uppercase
#include <iostream> int main() { //Turn on decimal notation std::cout << std::dec << 15 << '\n'; //Turn on hexidecimal notation std::cout << std::hex << 15 << '\n'; //Turn on octal notation std::cout << std::oct << 15 << '\n'; }
stream code snippets
Some code snippets one can use when working witj stream: