C++类中的动态数组
Dynamic array in class C++
我有一个类的成员是dynamic array(我在pastebin中发布了我的代码),我想知道我的两个类是否都是正确的,它们是否有什么问题?此外,我需要如何编写(41行)一个将类Student
设置为StudentsDynamicArray
的函数?
这是我的阵列类
class StudentsDynamicArray{
private:
Student *Stud;// students dynamic array
int n; // current size of array
int nmax; // max size of array
public:
StudentsDynamicArray():n(0), Stud(NULL), nmax(0){}
StudentsDynamicArray(int n):
n(n){}
~StudentsDynamicArray(); // destructor
void SetStud(Student S){Stud[n++] = S;} // I get error when I try to set String or Int. //How I need to change this?
Student GetStud(int i){return Stud[i];}
void IncreaseSize(int ns);
};
//Function which change size of array
void Student::ChangeSize(int kiek){
if(kiek > nmax){
int *SNEW = new int[kiek];
for(int i=0; i<n; i++)
SNEW[i] = mark[i];
delete [] mark;
mark = SNEW;
nmax = kiek;
}
else if(kiek < nmax){
int *SNEW = new int[kiek];
for(int i=0; i<n; i++)
SNEW[i] = mark[i];
delete [] mark;
mark = SNEW;
n = nmax = kiek;
}
}
在学生课堂上,你有
int *mark
这表明必须应用三条规则:复制构造函数、赋值运算符和析构函数。前两个应该复制动态数组,析构函数应该释放内存。您只正确地实现了析构函数。这同样适用于StudentsDynamicArray类。
对于:
void SetStud(Student S){Stud[n++] = S;}
从您的代码来看,您最初似乎没有调用IncreaseSize,一旦调用SetStud,Stud可能为NULL。你可以通过在构造函数中调用它来修复它:
StudentsDynamicArray():n(0), Stud(NULL), nmax(0){ IncreaseSize(10); }
此外,如果您使用std::vector,则上述所有内容都是不必要的,因此,如果您不需要这样的低杠杆动态阵列,则使用std::vector。然后你们会使用零规则。
[编辑]
您的复制构造函数/赋值运算符应该如下所示:
Student (const Student& other) {
// here allocate this->mark to be the same size as in other
// copy values from other.mark to this->mark
// also copy other values from `other` to this
}
Student& operator= (const Student& other)
{
// here allocate this->mark to be the same size as in other
// copy values from other.mark to this->mark
// also copy other values from `other` to this
return *this;
}
正如你所看到的,它们"非常"相似。关于"三条规则"的Wiki实际上更新到了C++11,在那里您可以添加移动语义,从而提高按值复制操作的效率(实际上被称为"五条规则")。如果你正在学习基础知识,你可以坚持以前的规则。
您可以更好地使用std::vector
将学生打包到集合中。这将显著提高性能,并减少错误。
下面是您可以使用的类的原型。
class Student
{
private:
string mName;
unsigned int mAge;
public:
Student() : mName(""), mAge(0) {}
Student( string name, unsigned int age ) : mName(name), mAge(age) {}
string get_name() const;
void set_name( string name );
unsigned int get_age() const;
void set_age( unsigned int age );
};
class Students
{
private:
vector<Student> mCollection;
public:
Students() : mCollection() { }
Students( vector<Student> &input_collection ) : mCollection( input_collection ) { }
Students &add_student( Student &s );
Students &remove_student( Student &s );
Students &remove_student( string name );
Students &remove_student( size_t index );
Students &edit_student( size_t index, string new_name, unsigned int new_age );
};
希望这能有所帮助。
相关文章:
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 输出没有重复元素的动态数组(收缩数组)C++
- 正在插入动态数组
- 如何在动态数组上使用搜索函数
- C++ 动态数组每次添加时将大小增加 1 - 错误
- 静态数组的自由动态数组
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 使用 thread 类在 C++ 中构造线程的动态数组时出错
- 如何为 c++ 的不同变量类型的结构元素创建动态数组?
- C++ 使用存储在动态数组中的文本文件中的数据查找模式
- 当我使用自定义类型创建动态数组时,即使使用字符串,它似乎也不起作用
- 为什么 c++ 动态数组的大小没有改变?
- 具有自定义构造函数 (C++) 的类型的动态数组分配
- 指针的 C++ 动态数组 - 何时需要使用它?
- 删除动态数组时未定义标识符
- 哈希映射使用 nullptr c++ 初始化节点的动态数组
- 如何将字符串和整数读取到两个单独的动态数组中的程序编写?
- 动态数组的 C++ 重载加运算符
- 基于数组的列表 - 动态数组创建时出错