Operators are helper functions that enable code to control input/output streams using operator<< or operator>>.
Manipulators called without arguments (such as std::cout << std::boolalpha; or std::cin >> std::hex; ) are implemented as functions that accept a reference to a stream as their only argument. Special overloaded versions of basic_ostream::operator<< and basic_istream::operator>> accept pointers to these functions. These functions (or instantiations of function templates) are the only addressable functions in the standard library. (since C++20)
Manipulators called with arguments (such as std::cout << std::setw(10); ) are implemented as functions that return an object of unspecified type. These operators define their own operator<< or operator>> that performs the requested operation.
Defined in the header file <iomanip>
Change floating point precision
std::setprecision
defined in header file |
||
/*unspecified*/ setprecision( int n ); |
When used in the expression out << setprecision(n) or in >> setprecision(n) , the set stream or out
argument is exactly .in
precision
n
parameter
n | - | new value for precision |
return value
Returns an object of unspecified type such that if str
is an output stream name of type std::basic_ostream<CharT, Traits> or an input stream name of type std::basic_istream<CharT, Traits>, the expression str << setprecision(n) Or str >> setprecision(n) behaves like executing the following code:
str.precision(n);
Call example
#include <iostream>
#include <iomanip>
#include <cmath>
#include <limits>
int main()
{
const long double pi = std::acos(-1.L);
std::cout << "default precision (6): "
<< pi << std::endl
<< "std::setprecision(10): "
<< std::setprecision(10) << pi << std::endl
<< "max precision: "
<< std::setprecision(std::numeric_limits<long double>::digits10 + 1)
<< pi << std::endl;
return 0;
}
output
Change the width of the next input/output field
std::setw
/*unspecified*/ setw( int n ); |
When used in the expressions out << setw(n) or in >> setw(n) , the set flow or out
argument is exactly .in
width
n
parameter
n | - | new value for width |
return value
Returns an unspecified type object. If str
it is the name of a std::basic_ostream<CharT, Traits> or std::basic_istream<CharT, Traits> type stream, the expression str << setw(n) or str >> setw(n ) behaves as if the following code were executed:
str.width(n);
Notice
If any of the following functions are called, the stream's width property will be set to zero (meaning "not specified"):
- enter
- operator>>(basic_istream&, basic_string&)
- operator>>(basic_istream&, char*)
- output
- Overloads 1-7 of basic_ostream::operator<<() (in stage 3 of num_put::put())
- operator<<(basic_ostream&, char) 和 operator<<(basic_ostream&, char*)
- operator<<(basic_ostream&, basic_string&)
- std::put_money (inside money_put::put())
- std::quoted (when used as an output stream)
The exact effect of this modifier on input and output varies between individual I/O functions and is described separately on each operator<<
overloaded operator>>
page.
Call example
#include <sstream>
#include <iostream>
#include <iomanip>
int main()
{
std::cout << "no setw:" << 42 << std::endl
<< "setw(6):" << std::setw(6) << 42 << std::endl
<< "setw(6), several elements: " << 89
<< std::setw(6) << 12 << 34 << std::endl;
std::istringstream is("hello, world");
char arr[10];
is >> std::setw(6) >> arr;
std::cout << "Input from \"" << is.str()
<< "\" with setw(6) gave \""
<< arr << "\"" << std::endl;
return 0;
}