Stream Manipulators and Iterators in C++
- Built-in Manipulators
- Custom Manipulators for User-Defined Objects
- User-Defined Inserters and Extractors
Now that you have learned (from our previous article in this series) how to create user-defined extractors and inserters to help facilitate the process of reading, writing, storing, and retrieving user-defined objects, let's take a look at how defining manipulators for user-defined objects helps in the formatting of those objects when inserted or extracted from streams.
To take full advantage of the stream processing model in C++ requires three basic steps:
Decide how the user-defined class needs to be translated during stream insertion or extraction.
Identify the stream states that are necessary for the proper translation of your user-defined class; then define manipulators and user-defined flags that will accommodate those stream states.
Define the inserters and the extractors for the user-defined class.
Examples of stream states include numeric bases (hexadecimal, octal, etc.) for a stream of numbers, or whitespace removal or insertion of NULL for a stream of characters. For our user-defined course class, a stream state would be horn clauses or XML formatting. The manipulators are used by the inserters and extractors to determine how the user-defined objects are translated and formatted for the stream.
Built-in Manipulators
Manipulators are functions or function objects that are inserted into or extracted from a stream; they affect the formatting of the objects that are used with that stream, or affect the stream itself in some way. Manipulators are used to control and set the state of the stream.
Table 1 lists commonly used built-in manipulators.
Manipulator |
Description |
endl |
Outputs a newline character and flushes the stream. |
ends |
Outputs a null character. |
flush |
Flushes a stream. |
dec |
Converts numeric values to decimal. |
hex |
Converts numeric values to hexadecimal. |
oct |
Converts numeric values to octal. |
resetiosflags(value) |
Turns off flags specified by value. |
setbase(value) |
Sets the number base to value. |
setfill(value) |
Sets the fill character to value. |
setiosflags(value) |
Turns on flags specified by value. |
setprecision(value) |
Sets the precision to value. |
setw(value) |
Sets the field width to value. |
Manipulators are used directly in the stream; for instance, the setfill, setw, and endl manipulators used in Listing 1 determine how the value of Programming101.startTime() will be formatted in the stream.
Listing 1 Using the Built-in Manipulators setfill, setw, and endl with a User-Defined Object
int main(int argc, char *argv[]) { ... course Programming101; Programming101.startTime("1200"); cout << setfill('*') << setw(7) << Programming101.startTime() << endl; ... return(0); }
The setw() manipulator requires that the next value to be inserted into the stream have a minimum width of 7. The setfill manipulator requires that the asterisk (*) be used for any necessary padding. The endl manipulator inserts a newline (\n) character in the stream. The manipulators in Listing 1 collectively will cause the following to be inserted into the cout stream:
***1200
NOTE
In this case, the value is right-aligned by default. Left alignment could have been specified using the setiosflags manipulator.