这里我有一个Excel文件,其第一列有ID,即:ID 12 32 45 12 ..还有其他列,但我只想读取第一列中的数据,即ID。
这是我抛出异常的代码。我不知道为什么?
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include<string>
#include<cstdlib>
//std::find
#include<cstring>
using namespace std;
int main()
{
ifstream fin("1.csv");
string line;
int rowCount = 0;
int rowIdx = 0; //keep track of inserted rows
//count the total nb of lines in your file
while (getline(fin, line)) {
rowCount++;
}
//this will be your table. A row is represented by data[row_number].
//If you want to access the name of the column #47, you would
//cout << data[0][46]. 0 being the first row(assuming headers)
//and 46 is the 47 column.
//But first you have to input the data. See below.
std::vector<std::vector<std::string>> data;
fin.clear(); //remove failbit (ie: continue using fin.)
fin.seekg(fin.beg); //rewind stream to start
while (getline(fin, line)) //for every line in input file
{
stringstream ss(line); //copy line to stringstream
string value;
while (getline(ss, value, ',')) { //for every value in that stream (ie: every cell on that row)
data[rowIdx].push_back(value);//add that value at the end of the current row in our table
}
rowIdx++; //increment row number before reading in next line
}
fin.close();
//Now you can choose to access the data however you like.
//If you want to printout only column 47...
int colNum;
string colName = "ID";
//1.Find the index of column name "computer science" on the first row, using iterator
//note: if "it == data[0].end()", it means that that column name was not found
vector<string>::iterator it = find(data[0].begin(), data[0].end(), colName);
//calulate its index (ie: column number integer)
colNum = std::distance(data[0].begin(), it);
//2. Print the column with the header "computer science"
for (int row = 0; row < rowCount; row++)
{
cout << data[row][colNum] << "\t"; //print every value in column 47 only
}
cout << endl;
return 0;
}
请帮助我解决这个问题。我只想显示包含ID的第一列。
2条答案
按热度按时间jogvjijk1#
下面是上面代码的简化版本,它去掉了二维向量,只读取第一列。
编辑
如何显示数据
ipakzgxi2#
问题在于:
配置
vector
的vector
。两个维度目前都设定为0。推入内部的
vector
并可能调整其大小,但外部向量的大小仍为0。rowIdx
的值无效。最简单的解决方案是使用rowCount
来调整外部vector
的大小,但结果证明这是一种浪费。您可以组装整个行,然后将行push_back
为data
,但即使这样也是浪费,因为只需要一个柱。向量的好处之一是你不必知道行数。你创建一个行向量,将列推入向量,然后将行推入数据。当你到达文件的末尾时,你就完成了阅读。不需要读取文件两次,但是你可能会在数据的最后一次自我调整大小上浪费一点存储空间。