c语言容器固态存储_学习c将类对象存储在容器中

c语言容器固态存储

The objects instantiated from a class can be stored in various C++ containers in the same way containers store variables of the various built-in data types. In this article I’m going to show you how to store class objects in arrays and vectors and how to access those objects once they are stored away.

从类实例化的对象可以以容器存储各种内置数据类型的变量的相同方式存储在各种C ++容器中。 在本文中,我将向您展示如何将类对象存储在数组和向量中,以及如何在这些对象被存储后访问这些对象。

类定义 (A Class Definition)

The class I’m going to use to demonstrate how to store class objects in container is a Student class that has the student’s name, their student id, and a vector with their grades for the term. It’s especially important that I have a member variable that is itself a container so I can show you how to access the grades from inside another container (that just got confusing).

我将用来演示如何将类对象存储在容器中的班级是一个Student班级,该班级具有学生的姓名,他们的学生ID和一个带有该学期成绩的向量。 拥有一个成员变量本身就是一个容器,这一点尤其重要,这样我可以向您展示如何从另一个容器(刚刚引起混淆)内部访问成绩。

Here is the Student (nominal) class definition:

这是Student (标称)类的定义:

class Student {
private:
string name;
string id;
vector<int> grades;public:
Student(string n, string i) {
name = n;
id = i;
} Student() {
name = "";
id = "";
} string getName() { return name; }
string getId() { return id; } void addGrade(int grade) {
grades.push_back(grade);
}
};

在数组中存储和访问类对象 (Storing and Accessing Class Objects in an Array)

Now let’s create a few objects and put them in an array. The array is created by using the Student class as the data type followed by a name and the number of elements to store. Here is the line of code for that:

现在,让我们创建一些对象并将它们放入数组中。 通过使用Student类作为数据类型,后跟名称和要存储的元素数来创建数组。 这是该行的代码:

const int numStudents = 3;
Student students[numStudents];

Here is the complete program that uses the Student class definition:

这是使用Student类定义的完整程序:

int main ()
{
Student stu1("Jane Smith", "1234");
stu1.addGrade(77);
stu1.addGrade(81);
stu1.addGrade(88);
Student stu2("John Jones", "2345");
stu2.addGrade(91);
stu2.addGrade(81);
stu2.addGrade(66);
Student stu3("Barb Green", "4321");
stu3.addGrade(81);
stu3.addGrade(82);
stu3.addGrade(83);
const int numStudents = 3;
Student students[numStudents];
students[0] = stu1;
students[1] = stu2;
students[2] = stu3;
return 0;
}

Now let’s see how to access the objects in the array to display their names and ids. If a Student object is not in a container, we can access the object name followed by the dot operator and the member function name we wanted. With the objects in an array, the object name is replaced with the array name and the index position we are accessing.

现在,让我们看看如何访问数组中的对象以显示其名称和ID。 如果Student对象不在容器中,我们可以访问对象名称,后跟点运算符和所需的成员函数名称。 对于对象在数组中,对象名称将替换为数组名称和我们正在访问的索引位置。

Here’s a code fragment that demonstrates how to do this:

这是一个代码片段,演示如何执行此操作:

for (int i = 0; i < numStudents; i++) {
cout << students[i].getName() << ", "
<< students[i].getId() << endl;
}

Here is the output when we run the program:

这是我们运行程序时的输出:

Jane Smith, 1234
John Jones, 2345
Barb Green, 4321

That wasn’t too bad. Now let’s go a level deeper and access the grades that are stored in the vector. Since the grades vector is private, we need a getter member function to retrieve the grades:

那还不错。 现在让我们更进一步,访问存储在向量中的成绩。 由于grades向量是私有的,因此我们需要一个getter成员函数来检索成绩:

void showGrades() {
for (int grade : grades) {
cout << grade << " ";
}
}

Now we can incorporate a call to this member function into our program:

现在,我们可以将对该成员函数的调用合并到我们的程序中:

for (int i = 0; i < numStudents; i++) {
cout << students[i].getName() << ", "
<< students[i].getId() << endl;
cout << "Grades: ";
students[1].showGrades();
cout << endl;
}

The output now is:

现在的输出是:

Jane Smith, 1234
Grades: 91 81 66
John Jones, 2345
Grades: 91 81 66
Barb Green, 4321
Grades: 91 81 66

Similarly, we could write member functions to calculate the grade average, find the lowest and highest grades, etc. But that’s enough with arrays for now. Let’s move on to vectors.

同样,我们可以编写成员函数来计算平均成绩,找到最低和最高成绩,等等。但是对于数组而言,这已经足够了。 让我们继续介绍向量。

在向量中存储和访问类对象 (Storing and Accessing Class Objects in a Vector)

The primary difference in using vectors for storing class objects versus using an array, other than all the differences between vectors and arrays, is how you declare the vector to store class objects.

除了矢量和数组之间的所有差异之外,使用矢量存储类对象与使用数组的主要区别在于如何声明矢量以存储类对象。

We can use the same Student class definition as shown earlier. We can use the same object instantiation code we used earlier to create three students. The first change we have to make is to replace the array declaration statement with a statement creating a vector:

我们可以使用前面显示的相同的Student类定义。 我们可以使用与之前创建三个学生相同的对象实例化代码。 我们要做的第一个更改是将数组声明语句替换为创建矢量的语句:

vector<Student> students;

We don’t need a constant to represent the number of students because we can use the size member function if we need to know how many elements are in the vector, and since we can use a range for loop for traversing the vector, we don’t need the constant for that loop.

我们不需要一个常数来表示学生的数量,因为我们可以利用size成员函数,如果我们需要知道有多少元素是矢量,因为我们可以用一系列for循环遍历向量,我们不不需要该循环的常数。

The rest of our program can be used as we wrote it earlier. We will use the same dot notation to access the member functions of the object, replacing array notation (students[n]) with the vector name, such as when we add students to the vector:

该程序的其余部分可以按我们先前的编写使用。 我们将使用相同的点符号来访问对象的成员函数,用向量名称替换数组符号( students[n] ),例如,当我们将向量添加到学生时:

students.push_back(stu1);
students.push_back(stu2);
students.push_back(stu3);

With those things in mind, here is the program for storing and accessing Student objects in a vector:

考虑到这些因素,下面是用于在向量中存储和访问Student对象的程序:

#include <iostream>
#include <vector>
using namespace std;// class definition goes hereint main ()
{
Student stu1("Jane Smith", "1234");
stu1.addGrade(77);
stu1.addGrade(81);
stu1.addGrade(88);
Student stu2("John Jones", "2345");
stu2.addGrade(91);
stu2.addGrade(81);
stu2.addGrade(66);
Student stu3("Barb Green", "4321");
stu3.addGrade(81);
stu3.addGrade(82);
stu3.addGrade(83);
vector<Student> students;
students.push_back(stu1);
students.push_back(stu2);
students.push_back(stu3);
for (Student stdnt : students) {
cout << stdnt.getName() << ", " << stdnt.getId() << endl;
cout << "Grades: ";
stdnt.showGrades();
cout << endl;
}
return 0;
}

Here is the output from the program:

这是程序的输出:

Jane Smith, 1234
Grades: 77 81 88
John Jones, 2345
Grades: 91 81 66
Barb Green, 4321
Grades: 81 82 83

班级开除 (Class Dismissed)

That’s it for this overview on storing class objects in array and vector containers. In my next article, I’ll move to a more technical yet useful topic — operator overloading.

关于将类对象存储在数组和向量容器中的概述,仅此而已。 在我的下一篇文章中,我将转到一个技术性更高但更有用的主题-操作符重载。

Thanks for reading and please email me at mmmcmillan1@att.net with comments and suggestions. If you are interested in my online programming courses, please visit https://learningcpp.teachable.com.

感谢您的阅读,请给我发电子邮件mmmmmillan1@att.net并提供评论和建议。 如果您对我的在线编程课程感兴趣,请访问https://learningcpp.teachable.com

翻译自: https://levelup.gitconnected.com/learning-c-storing-class-objects-in-containers-ca12546f1a89

c语言容器固态存储

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值