gpt4 book ai didi

c++ - 使用 openmode ios_base::out 的 istringstream 有什么意义

转载 作者:行者123 更新时间:2023-11-30 04:48:08 26 4
gpt4 key购买 nike

istringstream 的目的是什么?构造函数参数 openmode

特别是指定 ios_base::out 有什么意义吗?因为我认为这个对象从不支持流插入 <<运算符(operator)?

最佳答案

具有ios_base::in 访问模式的流不支持任何输出操作。如果未指定 ios_base::out,则更改序列的方法将失败。

来自 cppreference

Open mode: Access given by the internal stringbuf object to its internal sequence of characters.

ios_base::out - output - The sequence supports output operations.

ios_base::in is always set for istringstream objects (even if explicitly not set in argument which). Note that even though istringstream is an input stream, its internal stringbuf object may be set to also support output operations. This influences certain operations, such as putback, that in istringstream may alter the contents of the sequence.

参见 putback example :

        std::istringstream s1("Hello, world", std::ios_base::out); //  stream supporting output operations
s1.get();
if (s1.putback('Y')) // modifies the buffer
std::cout << s1.rdbuf() << '\n';
else
std::cout << "putback failed\n";

std::istringstream s2("Hello, world"); // input-only stream
s2.get();
if (s2.putback('Y')) // cannot modify input-only buffer
std::cout << s2.rdbuf() << '\n';
else
std::cout << "putback failed\n";

s2.clear();
if (s2.putback('H')) // non-modifying putback is OK
std::cout << s2.rdbuf() << '\n';
else
std::cout << "putback failed\n";

关于c++ - 使用 openmode ios_base::out 的 istringstream 有什么意义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55986429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com