C++ ios Library - Showpos Function


Advertisements

Description

It is used to sets the showpos format flag for the str stream. When the showpos format flag is set, a plus sign (+) precedes every non-negative numerical value inserted into the stream (including zeros).

Declaration

Following is the declaration for std::showpos function.

ios_base& showpos (ios_base& str);

Parameters

str − Stream object whose format flag is affected.

Return Value

It returns Argument str.

Exceptions

Basic guarantee − if an exception is thrown, str is in a valid state.

Data races

It modifies str. Concurrent access to the same stream object may cause data races.

Example

In below example explains about std::showpos function.

#include <iostream>

int main () {
   int p = 1;
   int z = 0;
   int n = -1;
   std::cout << std::showpos   << p << '\t' << z << '\t' << n << '\n';
   std::cout << std::noshowpos << p << '\t' << z << '\t' << n << '\n';
   return 0;
}

Let us compile and run the above program, this will produce the following result −

+1      +0      -1
1       0       -1
ios.htm
Advertisements