使用向量结构读取csv文件 [英] Reading a csv file using structures of vectors

查看:95
本文介绍了使用向量结构读取csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要将csv文件读入我的结构和向量,并根据列来排序......来自heatNo,distance和time。

我的代码问题是运行后,我的矢量仍然是空的,如果我不知道如何分拣后的数据



我尝试过:



I am to read a csv file into my structures and vectors and sort it according to the columns...from heatNo, distance and time.
The problem with my code is that after running, my vector remains empty and if i don't know how to cout the data after sorting

What I have tried:

#include <fstream>
#include <iostream>
#include <string>
#include <sstream> //stringstream
#include <vector>
#include <algorithm> //for sort()
using namespace std;

struct Details{
	string name;
	string dob;
	int heatNo;
	int distance;
	double time;
	string status;
};
bool multipleFields(const Details &lhs, const Details &rhs)
{
	if (lhs.heatNo < rhs.heatNo)
		return true;
	else if (lhs.distance < rhs.distance)
		return true;
	else if (lhs.time < rhs.time)
		return true;
}
int main()
{
	vector <details> results;
	ifstream inputFile ("C:\\Users\\mitch\\Desktop\\Data Structures\\projectInput.csv");
	Details swimmer;
	string line;
	
	while (!inputFile.eof() && getline(inputFile, line, '|'))
	{
		cout << line << "\t";
		stringstream ss(line);
		Details swimmer;
		for (int i = 0; i < 1e4; i++)
		{
			if (ss >> swimmer.name >> swimmer.dob >> swimmer.heatNo >> swimmer.distance >> swimmer.time >> swimmer.status)
			{
				results.push_back(swimmer);
			}
		}
	}
	inputFile.close();  //closes the file 
	cout << results.size();
	sort(results.begin(), results.end(), multipleFields); 
	system("pause");
	return 0;
}

推荐答案

Quote:

vector< details>结果;

vector <details> results;

以上行应该阻止正确编译你的代码。



如果你的向量是在执行循环后为空,然后在控制表达式或(全部)字符串流中出现问题提取操作员调用(为什么这么多调用?)。您可以调试或记录发生的事情。

The above line should prevent correct compilation of your code.

If your vector is empty after execution of the while loop then something went wrong either in the while control expression or in (all) the stringstream extraction operator calls (why so many calls?). You may either debug or log what happens.


这篇关于使用向量结构读取csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆