简体   繁体   English

从文本文档中获取不同的数据类型并将其存储到向量中? C++

[英]Getting different data types from a text document and storing it into vectors? C++

I have an assignment I am supposed to complete and really need some help with grasping and understanding certain concepts.我有一项任务我应该完成,并且确实需要一些帮助来掌握和理解某些概念。

Basically our task is to create a document called students.txt, where we will store a bunch of student information via this format:基本上我们的任务是创建一个名为students.txt的文档,我们将通过这种格式存储一堆学生信息:

Example:例子:


1387 History 4.0 1387历史4.0

3984 Science 2.3 3984 科学 2.3


The program will basically be able to create new students via inputting their ID, major and GPA by the user, and store it on a line underneath.该程序基本上可以通过用户输入他们的ID,专业和GPA来创建新学生,并将其存储在下面的一行中。 I am having some issues with understanding how I would format certain things.我在理解如何格式化某些内容方面遇到了一些问题。 I currently have this:我目前有这个:

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

int main(){
   std::ofstream studentFile{ "students.txt", std::ios::app };
   int studentID;
   string major;
   double gpa;

   cout << "ID #: ";
   cin >> studentID;

  cout << "Major: ";
  cin >> major;

  cout << "GPA: ";
  cin >> gpa;

  studentFile << studentID << " ";
  studentFile << major << " ";
  studentFile << gpa << " " << endl;

  studentFile.close();

  return 0;
}

I am also concerned on how I would do a feature of the program which requires you to display the given contents of the file if the user inputs to do so.我还担心我将如何执行程序的一项功能,该功能要求您在用户输入时显示文件的给定内容。 The hint is that when the program runs, the main function will have vectors, and each vector will represent each column of the document that can be used later on.提示是程序运行时,主function会有向量,每个向量代表文档的每一列,以后可以使用。 This is what i cannot grasp about the program, how each vector will read and know what to store and how, so that later if the user asks to remove a student via only by their ID, the rest of the information concerning that ID will be deleted as well.这是我无法理解的程序,每个向量将如何读取并知道要存储什么以及如何存储,以便以后如果用户仅通过他们的 ID 要求删除学生,则有关该 ID 的信息的 rest 将是也被删除了。

Such as: Remove student: 3984如:删除学生:3984

Or when asked the user asks to display the current information on the document it shows:或者当被问及用户要求在它显示的文档上显示当前信息时:

3728 Biology 3.6 8372 Math 2.4 2933 Science 3.4 3728 生物学 3.6 8372 数学 2.4 2933 科学 3.4

For the displaying part, I have this:对于显示部分,我有这个:

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <vector> 
using namespace std;

int main(){
  ifstream studentFile;
  studentFile.open("students.txt");


  vector<int> student_ids;
  std::vector<std::string> student_majors;
  vector<float> student_gpas;

  int data;

  while (studentFile >> data) {
    student_ids.push_back(data);
  }

  studentFile.close();
  cout << student_ids[0] << endl;
  cout << student_ids[1] << endl;
  cout << student_ids[2] << endl;

  return 0;
}

I know this isn't correct, but am trying to grasp how I would get data from a text document and store each data into a specific vector, so that the ID's in the document would be stored in vector student_ids, majors in the major vector etc.我知道这是不正确的,但我试图掌握如何从文本文档中获取数据并将每个数据存储到特定的向量中,以便文档中的 ID 将存储在向量 student_ids 中,major 向量中的专业等等

Any help or tips would mean the world, thank you for your time:)任何帮助或提示都意味着世界,谢谢你的时间:)

It's a mistake to make 3 vectors.制作3个向量是错误的。 The better way would be to produce a Student class:更好的方法是生成一个学生 class:

class Student {
public:
     int id;
     std::string major;
     double gpa;
};

Then you can define your vector:然后你可以定义你的向量:

std::vector<Student> students;

This way, you can keep all the data about a single student in a tidy little package.这样,您可以将有关单个学生的所有数据保存在一个整洁的小 package 中。

Then you need to have a for-loop that reads lines from your input file, sticks the data into a Student object, and then pushes that into the vector.然后,您需要有一个 for 循环,从输入文件中读取行,将数据粘贴到 Student object 中,然后将其推送到向量中。

Start with this structure for your code and see if you get further.从你的代码的这个结构开始,看看你是否能走得更远。


However, you've talked about iterating through three vectors.但是,您已经讨论过遍历三个向量。

vector<int> vec1;
vector<string> vec2;
vector<double> vec3;

... Assume you populate them equally so they're all the same size

for (size_t index = 0; index < vec1.size(); ++index) {
    int thisA = vec1.at(index);
    string thisStr = vec2.at(index);
    double thisDouble = vec3.at(index);
}

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

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