有没有办法结合两个变量?

时间:2020-05-02 13:14:51

标签: c++ class variables combinations stdvector

我一直试图让我的代码动态地将类对象分配给文件以供以后读取,但是在获取用户输入以保存到每个不同对象中时遇到了麻烦。

我正在尝试让用户输入他们的姓名,年龄和电话号码,并将其保存到文件中,希望以后可以使用相同的方法来读取该文件。

我尝试使用数组,但是不能保存对象的所有三个字段。有动态变量可以使用吗?

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

string mName, mID, mPhoneNumber;
int id = 0;
class Student
{
public:
   string mName;
   string mId;
   string mPhoneNumber;

   Student(string id = "", string name = "", string phone = "") : mId(id), mName(name), mPhoneNumber(phone)
   {}

   bool operator==(const Student& obj)
   {
      return (mId == obj.mId) && (mName == obj.mName) && (mPhoneNumber == obj.mPhoneNumber);
   }

   /*
    * Write the member variables to stream objects
    */
   friend ostream& operator << (ostream& out, const Student& obj)
   {
      out << obj.mId << "\n" << obj.mName << "\n" << obj.mPhoneNumber << endl;
      return out;
   }
   /*
    * Read data from stream object and fill it in member variables
    */
   friend istream& operator >> (istream& in, Student& obj)
   {
      in >> obj.mId;
      in >> obj.mName;
      in >> obj.mPhoneNumber;
      return in;
   }
};

int main()
{
   cin >> id;
   Student stud1("1", "Jack", "4445554455");
   Student stud2("4", "Riti", "4445511111");
   Student stud3("6", "Aadi", "4040404011");

   // open the File
   ofstream out("students.txt");
   // Write objects to file (targets to cout)
   out << stud1;
   out << stud2;
   out << stud3;

   out.close();
   // Open the File
   ifstream in("students.txt");
   Student student1;
   Student student2;
   Student student3;
   // Read objects from file and fill in data
   in >> student1;
   in >> student2;
   in >> student3;
   in.close();
   // Compare the Objects
   assert(stud1 == student1);
   assert(stud2 == student2);
   assert(stud3 == student3);

   cout << stud1 << endl;
   cout << stud2 << endl;
   cout << stud3 << endl;
   return 0;
}

3 个答案:

答案 0 :(得分:2)

您可以通过以下方式使用std::vector

std::vector<Student> my_students;
for (std::size_t i = 0; i < 3; i++) {
    Student tmp;
    in >> tmp;
    my_students.push_back(tmp);
}

答案 1 :(得分:1)

javax.validation.constraints

答案 2 :(得分:1)

您可以使用std::vector来存储Student,并对其进行迭代以输出/输入。

#include <vector>

int main()
{
   // open the File
   std::fstream file{ "students.txt" };

   // vector of students
   std::vector<Student> students{
      {"1", "Jack", "4445554455"},
      { "4", "Riti", "4445511111"},
      {"6", "Aadi", "4040404011"}
   };

   // iterate throut the objects and write objects(i.e. students) to the file
   for(const auto& student: students)
      file << student;

   // reset the stream to the file begin
   file.clear();
   file.seekg(0, ios::beg);

   // clear the vector and resize to the number of objects in the file
   students.clear();
   students.resize(3);

   // read objects from file and fill in vector
   for (Student& student : students)
      file >> student;

   file.close();
   return 0;
}