Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) stream

 

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

 

 

 

 

 

Global streams

 

Examples of global streams are:

 

 

 

 

 

stream (data) types

 

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:

 

 

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:

 

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

 

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict