C++ 检测何时在字符串流的末尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20572203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:12:34 来源:igfitidea点击:
Detect when at the end of a stringstream
提问by JNevens
I am trying to write a function which will detect when I'm almost at the end of a stringstream, but it doesn't seem to work.
我正在尝试编写一个函数,该函数将检测何时我几乎处于字符串流的末尾,但它似乎不起作用。
Here's some code:
这是一些代码:
std::string path(1.2.3);
int number;
std::stringstream ss(path);
while (!ss.eof()) {
if (ss.peek() != '.') {
ss >> number;
if (ss.tellg() == path.length()) {
std::cout << "Last one: " << number;
} else {
std::cout << number;
}
} else { ss.get(); }
}
I tried using ss.tellg() == path.length(), but that doesn't work. Does somebody have an alternative?
我尝试使用 ss.tellg() == path.length(),但这不起作用。有人有替代方案吗?
采纳答案by JNevens
I figured it out..
我想到了..
std::string path(1.2.3);
int number;
std::stringstream ss(path);
while (!ss.eof()) {
if (ss.peek() != '.') {
ss >> number;
if (ss.tellg() == -1) {
std::cout << "Last one: " << number;
} else {
std::cout << number;
}
} else { ss.get(); }
}