Skip to content

Latest commit

 

History

History
137 lines (99 loc) · 7.61 KB

CppStream.md

File metadata and controls

137 lines (99 loc) · 7.61 KB

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:


bool b: 1 char c: c integer i: 1 double d: 1.1

 

Examples of global streams are:

 

Types of streams are:

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:


#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: