使用带有">>"运算符的 std::istringstream 的奇怪行为
Strange behaviour using std::istringstream with '>>' operator
我注意到下面非常简单的程序有一个奇怪的行为。
#include <iostream>
#include <sstream>
#include <string>
int main(void)
{
std::string data = "o BoxModelnv 1.0f, 1.0f, 1.0fnv 2.0f, 2.0f, 2.0fn";
std::istringstream iss(data);
std::string line;
std::string type;
while (std::getline(iss, line, 'n'))
{
iss >> type;
std::cout << type << std::endl;
}
getchar();
return (0);
}
输出如下:
v
v
v
但我想要以下一个:
o
v
v
我尝试了这个解决方案:
#include <iostream>
#include <sstream>
#include <string>
int main(void)
{
std::string data = "o BoxModelnv 1.0f, 1.0f, 1.0fnv 2.0f, 2.0f, 2.0fn";
std::istringstream iss(data);
std::string line;
std::string type;
iss >> type;
std::cout << type << std::endl;
while (std::getline(iss, line, 'n'))
{
iss >> type;
std::cout << type << std::endl;
}
getchar();
return (0);
}
但输出如下:
o
v
v
v
请问有人可以帮助我吗?提前非常感谢您的帮助。
调用 getline 后,从字符串流的缓冲区中删除第一行。字符串中第一个换行符之后的单词是"v"。
在 while 循环中,使用该行作为输入创建另一个字符串流。现在从这个字符串流中提取你的类型单词。
while (std::getline(iss, line, 'n'))
{
std::istringstream iss2(line);
iss2 >> type;
std::cout << type << std::endl;
}
相关文章:
- EASTL矢量<向量<int>>连续的
- 如何使用iStringstream读取和解析文本文件?
- istringstream >>运算符产生意外行为
- 在C++中使用istringstream读取输入时出错
- istringstream,num1 和 num2 在这里发生了什么?
- 来自 istringstream 的五倍输出
- 是否可以在不指定类型的情况下从 istringstream 转到向量?
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 使用 string_view 构造 istringstream 无法编译
- 有没有办法我可以从 istringstream 中读取两个字符
- C 字符串比较“祝您好运”&gt;“再见”
- istringstream将字符串放回输入并再次读取
- 为什么 istringstream 在流的末尾附加'-1'字符?
- 字符串向量到istringstream
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- istringstream-function string_to_int不能接受C-STR,为什么
- 基于循环的C ISTRINGSTREAM范围没有开始成员
- 无法使用 istringstream 从.txt文件中读取
- 为什么当我将 std::locale 设置为 "zh_CN.UTF-8" 时 std::istringstream 失败?
- 修改 iStringStream 中的变量数