cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : IOstream Library : manipulators : setw
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
IOstream Library
manipulators
classes:
· filebuf
· fstream
· ifstream
· ios
· iostream
· ios_base
· istream
· istringstream
· ofstream
· ostream
· ostringstream
· streambuf
· stringbuf
· stringstream
objects:
· cerr
· cin
· clog
· cout
types:
· fpos
· streamoff
· streampos
· streamsize
manipulators
· boolalpha
· dec
· endl
· ends
· fixed
· flush
· hex
· internal
· left
· noboolalpha
· noshowbase
· noshowpoint
· noshowpos
· noskipws
· nounitbuf
· nouppercase
· oct
· resetiosflags
· right
· scientific
· setbase
· setfill
· setiosflags
· setprecision
· setw
· showbase
· showpoint
· showpos
· skipws
· unitbuf
· uppercase
· ws

-

setw manipulator function
smanip setw ( int n );
<iomanip>

Set field width

Sets the number of characters to be used as the field width for the next insertion operation.

Behaves as if a call to the stream's member ios_base::width with n as its argument was made.

The field width determines the minimum number of characters to be written in some output representations. If the standard width of the representation is shorter than the field width, the representation is padded with fill characters (see setfill) at a point determined by the format flag adjustfield (left, right or internal).

This manipulator is declared in header <iomanip>, along with the other parameterized manipulators: resetiosflags, setiosflags, setbase, setfill and setprecision. This header file declares the implementation-specific smanip type, plus any additional operator overload function needed to allow these manipulators to be inserted and extracted to/from streams with their parameters.

Parameters

n
Number of characters to be used as field width.

Return Value

Unspecified. This function should only be used as a stream manipulator.

Example

// setw example
#include <iostream>
#include <iomanip>
using namespace std;

int main () {
  cout << setw (10);
  cout << 77 << endl;
  return 0;
}

This code uses setw to set the field width to 10 characters.

See also

setfill Set fill character (manipulator function)
ios_base::width Get/set field width (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us