简体   繁体   English

读取.csv文件并将值存储到vector和struct c ++中

[英]Reading in a .csv file and storing values into vector and struct c++

I am new to programming in C++ and am trying to do a practice problem, but I don't really understand what it is trying to get me to do since a vector can only hold one data type (unless it can hold more): 我是C ++编程的新手,正在尝试解决一个实践问题,但是由于向量只能容纳一种数据类型(除非它可以容纳更多的数据类型),所以我并不真正理解它试图让我做什么:

Read in a .csv file where each line is structured as such: "username,gpa,age". 读取.csv文件,其中每行的结构如下:“用户名,gpa,年龄”。 Store these values into a struct with username, gpa, and age (string,float,int) and create a list as a vector. 将这些值存储到具有用户名,gpa和年龄(字符串,浮点数,整数)的结构中,并创建一个向量列表。 As you read in each line, insert into the vector list sorted by username. 在每一行中阅读时,请插入按用户名排序的向量列表。 Then loop through and print out the list formatted as : "username [gpa] age:#" eg, "mark [3.9] age:19" and also write the output to a file (using C++, not Unix). 然后循环遍历并打印出格式如下的列表:“用户名[gpa] age:#”,例如“ mark [3.9] age:19”,还将输出写入文件(使用C ++,而不是Unix)。

Am I supposed to put all of those values into the same vector as separate data types or keep them all together in a string and then insert them? 我是否应该将所有这些值作为单独的数据类型放入相同的向量中,或者将它们全部放在一个字符串中然后插入它们? How do I store these values into a struct? 如何将这些值存储到结构中? If someone can tell me how they would solve the question and give me some sample code that'd be great, thank you. 如果有人可以告诉我他们将如何解决问题并给我一些很好的示例代码,谢谢。

Ok, by the sound of it, the question wants you to read the file, put the information into a struct, then insert the structs into the vector in alphabetical order according to the name. 好吧,从声音的角度来看,问题是希望您读取文件,将信息放入结构中,然后根据名称按字母顺序将结构插入向量中。

I'd make the struct something like: 我将使结构类似于:

struct StudentInfo {
    string name;
    string gpa;
    string age;
} StudentInfo;

I'd then open the file and read the whole thing into a string. 然后,我将打开文件并将整个内容读入字符串。 Then I'd read through the string, tokenizing it by newline character. 然后,我通读了字符串,并用换行符将其标记。 As I get each string, I'd put it into a function that parsed it and returned a struct studentInfo. 当我得到每个字符串时,我会将其放入一个函数中,该函数对其进行解析并返回一个struct studentInfo。 then insert the struct into the vector. 然后将结构插入向量。

An example of how the parsing function might work: 解析功能如何工作的一个示例:

struct StudentInfo parseData(string iStr) {
    struct StudentInfo info;
    size_t subStrStart = 0;
    size_t subStrEnd = 0;
    subStrEnd = iStr.find(',', subStrEnd);
    info.name = iStr.substr(subStrStart, subStrStart - subStrEnd);

    subStrStart = subStrEnd;
    subStrEnd = iStr.find(',', subStrEnd+1);
    info.gpa = iStr.substr(subStrStart, subStrStart - subStrEnd);

    subStrStart = subStrEnd;
    subStrEnd = iStr.find(',', subStrEnd+1);
    info.age = iStr.substr(subStrStart, subStrStart - subStrEnd);

    return info;
}

Some pseudocode for the main function I'd make: 我为主要功能编写的一些伪代码:

vector<struct StudentInfo> infoVec;
string fileStr = read(filename.csv); // make an ifstream or something similar
string lineStr;
size_t subStrEnd = 0;
size_t subStrStart = 0;
while (subStrEnd < fileStr.Size) {
    subStrEnd = iStr.find('\n', subStrEnd); //alternatively use std::getline() directly on the file stream
    lineStr = iStr.substr(subStrStart, subStrStart - subStrEnd);
    subStrStart = subStrEnd;
    subStrEnd++;
    infoVec.insert(parseData(lineString));
}
sortByName(infoVec) // User defined function
printInfoVec(infoVec) // User defined function

The print function and the sort function are pretty self explanatory, they'd require you to write them though. 打印功能和排序功能很容易说明,但是它们需要您编写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM