我有以下代码:
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
stringstream buffer("1234567890 ");
cout << "pos-before: " << buffer.tellg() << endl;
buffer.ignore(10, ' ');
cout << "pos-after: " << buffer.tellg() << endl;
cout << "eof: " << buffer.eof() << endl;
}
它产生这个输出:pos-before: 0
pos-after: 11
eof: 0
我希望 pos-after
是 10
而不是 11
。根据规范,ignore 方法应在设置以下任一条件时停止:std::numeric_limits<std::streamsize>::max()
Traits::eq_int_type(Traits::to_int_type(c), delim)
确定。分隔符被提取并丢弃。如果 delim 是 Traits::eof()
在这种情况下,我希望规则 1 在所有其他规则之前触发,并在流位置为 10 时停止。
Execution 表明情况并非如此。我误解了什么?
我还尝试了代码的变体,其中我只忽略了 9 个字符。在这种情况下,输出是预期的:
pos-before: 0
pos-after: 9
eof: 0
所以看起来在 ignore()
提取字符数的情况下,它仍然检查下一个字符是否是 delimiter
,如果是,它也提取它。我可以使用
g++
和 clang++
进行复制。我也尝试过这种代码变体:
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
cout << "--- 10x get\n";
stringstream buffer("1234567890");
cout << "pos-before: " << buffer.tellg() << '\n';
for(int i=0; i<10; ++i)
buffer.get();
cout << "pos-after: " << buffer.tellg() << '\n';
cout << "eof: " << buffer.eof() << '\n';
cout << "--- ignore(10)\n";
stringstream buffer2("1234567890");
cout << "pos-before: " << buffer2.tellg() << '\n';
buffer2.ignore(10);
cout << "pos-after: " << buffer2.tellg() << '\n';
cout << "eof: " << buffer2.eof() << '\n';
}
结果是:--- 10x get
pos-before: 0
pos-after: 10
eof: 0
--- ignore(10)
pos-before: 0
pos-after: -1
eof: 1
我们看到使用 ignore()
会在文件上产生文件结束条件。表明 ignore()
在提取了 10 个字符后确实尝试提取一个字符。但在这种情况下,第三个条件被禁用,ignore()
不应该试图查看下一个字符是什么。
最佳答案
[istream.unformatted] 第 25 段中 std::basic_istream::ignore
的规范有点不清楚:它指出“字符被提取,直到出现以下任何一种情况:”没有任何顺序指示。第 25.1 段说明最多提取 n
字符(除非 n
是 std::numeric_limits<std::streamsize>
),第 25.3 段说明字符匹配。然而,即使条件可以以任何顺序应用,这里也不存在冲突:n
th 字符还不是预期的字符,ignore()
应该停止。
正如评论中指出的那样,在 libstdc++
中有一个 bug ,它似乎仍然存在于带有 gcc-10.2.0
的库中。将 clang++
与 libc++
一起使用(如有必要,在调用 -stdlib=libc++
时使用 clang++
)不会显示相同的行为。
顺便说一句:未格式化的输入操作正在设置可以使用 gcount()
访问的读取字符数。在流中查找是一种比访问此计数更昂贵的操作。使用 gcount()
也显示了这个问题(谈到昂贵的操作,我还使用 std::endl
代替了 '\n'
的使用;有关更多详细信息,请参阅 this video 或 this article ):
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::istringstream buffer("1234567890 ");
buffer.ignore(10, ' ');
std::cout << "gcount: " << buffer.gcount() << '\n';
std::cout << "eof: " << std::boolalpha << buffer.eof() << '\n';
}
关于c++ - 为什么 std::basic_istream::ignore() 提取的字符多于指定的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64204443/