Saturday, November 26, 2011

C++ Tutorial: Standard Manipulators

It is seen that for formatted input/output the stream class ios function are used. To call ios function for formatting, we need to write separate statement and call the function through stream objects cin and cout. Manipulators are the formatting function for input/output that are embedded directly into the C++ input/output statements so that extra statements are not required. Manipulators are used along with the extraction >> and insertion << operators for stream input and output formatting. According to the number of arguments to be supplied manipulators are categorized in the  following types.
  1. Non-parameterized Manipulators
  2. Parameterized Manipulators
Non parameterized Manipulators do no take argument to control the formatting of input/output where as parameterized manipulators take argument for formatting. The Following table shows different non parameterized manipulators.
Manipulator Effect produced
left sets ios::left flag of ios::adjustfield
right sets ios::right flag of ios::adjustfield
internal sets ios::internal flag of ios::adjustfield
dec sets ios::dec flag of ios::adjustfield
hex sets ios::hex flag of ios::adjustfield
oct sets ios::oct flag of ios::adjustfield
scientific sets ios::scientific flag of ios::floatfield
fixed sets ios::fixed flag of ios::floatfield
showbase sets ios::showbase flag
noshowbase resets ios::showbase flag
showpoint sets ios::showpoint flag
noshowpoint resets ios::showpoint flag
showpos sets ios::showpos flag
noshowpos resets ios::showpos flag
uppercase sets ios::uppercase flag
nouppercase resets ios::uppercase flag
skipws sets ios::skipws flag
noskipws resets ios::skipws flag
boolalpha sets ios::boolalpha flag
noboolalpha resets ios::boolalpha flag
unitbuf sets ios::unitbuf flag
nounitbuf resets ios::unitbuf flag
endl output newline and flush
ends ouput ‘\0’ character
flush flush the stream, same as ostream::flush
ws removes all white space from input stream until non white space character.
Following table shows different parameterized manipulators
Manipulators Effect produced
setw(int n) equivalent to ios function width()
setprecision (int n) equivalent to ios function precision()
setfill(int c) equivalent to ios::function fill()
setbase (int b) sets base for integer output, argument 0 is passed for decimal, 8 or octal, 10 for decimal and 16 for hexadecimal.
setiosflags(ios::fmtflags f) equivalent to ios function setf()
resetiosflags(ios::fmtflags f) equivalent to ios function unsetf()

No comments:

Post a Comment