【问题标题】:Is there a way to combine two variables?有没有办法结合两个变量?
【发布时间】:2020-05-02 13:14:51
【问题描述】:

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

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

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

#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;
}

【问题讨论】:

  • 您是否尝试过使用std::vector?
  • 我只是查了一下,但我仍在试图弄清楚在这种情况下如何使用它进行输入和输出
  • 旁注:您试图以错误的顺序初始化成员变量。它们将按照您声明它们的顺序进行初始化。

标签: c++ class variables combinations stdvector


【解决方案1】:

您可以通过以下方式使用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);
}

【讨论】:

【解决方案2】:
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();

【讨论】:

  • 我怀疑这是否有帮助,甚至根本没有用。为了说服我,请解释它是如何工作的以及它为什么有用。
【解决方案3】:

您可以使用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;
}

【讨论】:

    猜你喜欢
    • 2022-01-22
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多