C++标准模板(STL)- 输入/输出操纵符-(std::fixed, std::scientific, std::hexfloat, std::defaultfloat,std::ws)

操纵符是令代码能以 operator<< 或 operator>> 控制输入/输出流的帮助函数。

不以参数调用的操纵符(例如 std::cout << std::boolalpha; 或 std::cin >> std::hex; )实现为接受到流的引用为其唯一参数的函数。 basic_ostream::operator<< 和 basic_istream::operator>> 的特别重载版本接受指向这些函数的指针。这些函数(或函数模板的实例化)是标准库中仅有的可取址函数。 (C++20 起)

以参数调用的操纵符(例如 std::cout << std::setw(10); )实现为返回未指定类型对象的函数。这些操纵符定义其自身的进行请求操作的 operator<< 或 operator>> 。
 

更改用于浮点 I/O 的格式化

std::fixed, 
std::scientific, 
std::hexfloat, 
std::defaultfloat

std::ios_base& fixed( std::ios_base& str );

(1)

std::ios_base& scientific( std::ios_base& str );

(2)

std::ios_base& hexfloat( std::ios_base& str );

(3) (C++11 起)

std::ios_base& defaultfloat( std::ios_base& str );

(4) (C++11 起)

修改浮点输入/输出的默认格式化。

1) 如同以调用 str.setf(std::ios_base::fixed, std::ios_base::floatfield) ,设置流 strfloatfieldfixed

2) 如同以调用 str.setf(std::ios_base::scientific, std::ios_base::floatfield) ,设置流 strfloatfieldscientific

3) 如同以调用 str.setf(std::ios_base::fixed | std::ios_base::scientific, std::ios_base::floatfield) ,设置流 strfloatfield 同时为 fixedscientific 。这启用十六进制浮点格式化。

4) 如同以调用 str.unsetf(std::ios_base::floatfield) ,设置流 strfloatfield 为零,这异于 fixed 和 scientific 。

这是一个 I/O 操纵符,可用如 out << std::fixed 的表达式对任何 std::basic_ostream 类型的 out 或用如 in >> std::scientific 的表达式对任何 std::basic_istream 类型的 in 调用。

参数

str - 到 I/O 流的引用

返回值

str (到操纵后的流的引用)

注意

十六进制浮点格式化忽略流精度规定,如 std::num_put::do_put 的规定所要求。

 调用示例

#include <iostream>
#include <sstream>

int main()
{
    std::cout << "The number 0.01 in fixed:      "
              << std::fixed << 0.01 << std::endl
              << "The number 0.01 in scientific: "
              << std::scientific << 0.01 << std::endl
              << "The number 0.01 in hexfloat:   "
              << std::hexfloat << 0.01 << std::endl
              << "The number 0.01 in default:    "
              << std::defaultfloat << 0.01 << std::endl;

    double f;
    std::istringstream("0x1P-1022") >> std::hexfloat >> f;
    std::cout << "Parsing 0x1P-1022 as hex gives " << f << std::endl;

    return 0;
}

输出

消耗空白符

std::ws

template< class CharT, class Traits >
std::basic_istream<CharT,Traits>& ws( std::basic_istream<CharT, Traits>& is );

从输入流舍弃前导空白符。

表现为无格式输入函数 (UnformattedInputFunction) ,除了不修改 is.gcount() 。在构造并检查 sentry 对象后,从流释出并舍弃字符,直至出现任何下列条件之一:

  • 输入序列中出现文件尾条件(该情况下函数调用 setstate(eofbit) 但不设置 failbit ;若调用 ws 前已在 is 上设置 eofbit ,则不适用这条,该情况下 sentry 对象的构造会设置 failbit )。
  • 输入序列中下个可用字符 c 不是以 std::isspace(c, is.getloc()) 确定的空白字符。不释出该非空白字符。

这是仅为输入的 I/O 操纵符,可用如 in >> std::ws 的表达式对任何 std::basic_istream 类型的 in 调用。

参数

is - 到输入流的引用

返回值

is (到释出连续空白符后的流的引用)

注意

若在调用前在流上设置了 eofbit ,则 sentry 对象的构造将设置 failbit

调用示例

#include <iostream>
#include <istream>
#include <sstream>
#include <string>

int main()
{
    std::istringstream s("     this is a test");
    std::string line;
    std::getline(s >> std::ws, line);
    std::cout << "ws + getline returns: \""
              << line << "\"" << std::endl;

    return 0;
}

输出

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/133232928
今日推荐