【发布时间】:2015-09-30 17:23:43
【问题描述】:
我有一个使用纬度、经度和位置名称的文本格式 ,例如:
41.3333 34.3232 Old Building
我必须读取这个文本文件(从命令行),用空格分隔每一行,使用stod 将lat 和long 转换回double,然后读取整个文件成向量或列表。
这就是我目前所坚持的:
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
class Distance{
public:
double x;
double y;
string location;
};
int main(int argc, char *argv[]){
// If the user didn't provide a filename command line argument,
// print an error and exit.
if (argc <= 1){
cout << "Usage: " << argv[0] << " <Filename>" << endl;
exit(1);
}
char *pFilename = argv[1];
string buf; // Have a buffer string
stringstream ss(argv[1]); // Insert the string into a stream
vector<string> tokens; // Create vector to hold our words
while (ss >> buf)
tokens.push_back(buf);
}
问题:
- 我能否对如何继续实施有所了解?
答案:从这里我需要查看文件中的每一行并用空格分割它们,然后将文件存储在一个向量中。所以文本文件的第一个数字是纬度,第二个经度,第三个(字符串)是位置。
【问题讨论】:
-
您可以先考虑
fstream的输入及其各自的语法。 -
stringstream ss(argv[1]);只将文件名而不是文件内容放入ss。 -
你可以为
::operator>>重载Distance。
标签: c++ file text vector double