有没有办法把两个变量结合起来?

问题描述 投票:2回答:3

我一直想让我的代码动态地分配类对象到文件中,以便以后读取,但在让用户输入保存到每个不同的对象中时遇到了问题。

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

我试过使用数组,但那不能保存对象的三个字段。有没有一种动态变量可以使用?

#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;
}
c++ class variables combinations stdvector
3个回答
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
投票
std::vector<Student> aVectOfStudents;

aVectOfStudents.emplace_back("","Jack", "4445554455");
aVectOfStudents.emplace_back("","Riti", "4445511111");
aVectOfStudents.emplace_back("","Aadi", "4040404011");

ofstream out("students.txt");
for(auto studIter = aVectOfStudents.begin(); studIter != aVectOfStudents.end(); ++studIter)
{
    std::cout << "Insert Id for student: " << studIter->mName << "\n";
    std::cin >> studIter->mId;
    out<<*studIter;

}

out.close();

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.