distributionrefa.blogg.se

Istream class
Istream class













istream class

Note that if the precision is less than the number of significant digits, the number will be rounded. If fixed or scientific notation is used, precision determines how many decimal places in the fraction is displayed. Member function Meaning std::ios_base::precision() Returns the current precision of floating-point numbers std::ios_base::precision(int) Sets the precision of floating-point numbers and returns old precision Flags live in the std::ios class, manipulators live in the std namespace, and the member functions live in the std::ostream class. Here is a list of some of the more useful flags, manipulators, and member functions.

ISTREAM CLASS HOW TO

Many options are available via both flags and manipulators (such as changing the base), however, other options are only available via flags or via manipulators, so it’s important to know how to use both. In general, using manipulators is much easier than setting and unsetting flags. Std::cout << std::dec << 29 << '\n' // back to decimal Std::cout << 28 << '\n' // we're still in hex Here is an example of using some manipulators to change the base: std::cout << std::hex << 27 << '\n' // print 27 in hex The nice thing about manipulators is that they are smart enough to turn on and off the appropriate flags. Using setf() and unsetf() tends to be awkward, so C++ provides a second way to change the formatting options: manipulators. Std::tf(std::ios::hex, std::ios::basefield) For example: // Turn on std::ios::hex as the only std::ios::basefield flag When using this form of setf(), all of the flags belonging to the group are turned off, and only the flag passed in is turned on. The second way is to use a different form of setf() that takes two parameters: the first parameter is the flag to set, and the second is the formatting group it belongs to. Std::tf(std::ios::hex) // turn on hexadecimal output There are two ways to get around this problem.įirst, we can turn off std::ios::dec so that only std::hex is set: std::cout.unsetf(std::ios::dec) // turn off decimal output Consequently, when we turned std::hex on, std::ios::dec was still on, and std::ios::dec apparently takes precedence. It didn’t work! The reason why is because setf() only turns flags on - it isn’t smart enough to turn mutually exclusive flags off. Consequently, if we do this: std::tf(std::ios::hex) // try to turn on hex output For example, a format group named “basefield” contains the flags “oct”, “dec”, and “hex”, which controls the base of integral values. A format group is a group of flags that perform similar (sometimes mutually exclusive) formatting options. Many flags belong to groups, called format groups. There’s one other bit of trickiness when using setf() that needs to be mentioned. This results in the following output: +27 Std::cout.unsetf(std::ios::showpos) // turn off the std::ios::showpos flag To turn a flag off, use the unsetf() function: std::tf(std::ios::showpos) // turn on the std::ios::showpos flag It is possible to turn on multiple ios flags at once using the Bitwise OR (|) operator: std::tf(std::ios::showpos | std::ios::uppercase) // turn on the std::ios::showpos and std::ios::uppercase flag However, by using the std::ios::showpos flag, we can change this behavior: std::tf(std::ios::showpos) // turn on the std::ios::showpos flag For example, by default, C++ does not print a + sign in front of positive numbers.

istream class

To switch a flag on, use the setf() function, with the appropriate flag as a parameter. Manipulators are objects placed in a stream that affect the way things are input and output. You can think of flags as boolean variables that can be turned on and off. There are two ways to change the formatting options: flags, and manipulators. One of the jobs of ios (and ios_base) is to control the formatting options for output.

istream class

In the lesson on streams, you saw that both istream and ostream were derived from a class called ios. C++ has predefined insertion operations for all of the built-in data types, and you’ve already seen how you can overload the insertion operator for your own classes. The insertion operator (<<) is used to put information into an output stream. In this section, we will look at various aspects of the iostream output class (ostream).















Istream class