【发布时间】:2017-03-29 10:40:22
【问题描述】:
我是 C++ 新手,我正在尝试从带有数字的文件中读取行,将行标记为字符串数组并将这些数组项转换为双精度数字。但是在标记化的过程中,我得到了这个错误''变量'std :: stringstream mystream'有初始化程序但类型不完整''。我看过其他人的建议,我想在不使用 boost 的情况下做到这一点,其余代码看起来很像我的,但由于某种原因我得到了这个错误。这是代码。
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
vector<string> tokens;
string phrase="sdfs sdfs trt we rw";
stringstream mystream (phrase);
string temp;
while(getline(mystream,temp,' ')){
tokens.push_back(temp);
}
}
如果能就这个问题获得一些反馈,那就太好了。提前致谢。
【问题讨论】:
-
您没有包含正确的
std::stringstream标头。 -
在不相关的说明中,如果您只是从字符串流中读取,请考虑改用
std::istringstream, -
最后,顺便说一句,你不需要循环。您所需要的只是
std::istream_iterator和std::vectorconstructors 的知识。注意到带有一对迭代器的向量构造函数了吗?