读取 .csv 文件并将值存储到向量和结构 c++ 中 [英] Reading in a .csv file and storing values into vector and struct c++

查看:47
本文介绍了读取 .csv 文件并将值存储到向量和结构 c++ 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 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):

读入 .csv 文件,其中每一行的结构如下:username,gpa,age".将这些值存储到一个包含用户名、gpa 和年龄(字符串、浮点数、整数)的结构中,并创建一个列表作为向量.当您阅读每一行时,插入到按用户名排序的向量列表中.然后循环并打印出格式为:username [gpa] age:#"的列表,例如mark [3.9] age:19",并将输出写入文件(使用 C++,而不是 Unix).

Read in a .csv file where each line is structured as such: "username,gpa,age". Store these values into a struct with username, gpa, and age (string,float,int) and create a list as a vector. 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:#" e.g., "mark [3.9] age:19" and also write the output to a file (using C++, not 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.

我会让结构像这样:

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

然后我会打开文件并将整个内容读入一个字符串.然后我通读字符串,用换行符标记它.当我获得每个字符串时,我会将它放入一个函数中,该函数对其进行解析并返回一个结构 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. 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

print 函数和 sort 函数很容易解释,不过它们需要您编写.

The print function and the sort function are pretty self explanatory, they'd require you to write them though.

这篇关于读取 .csv 文件并将值存储到向量和结构 c++ 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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