C++数组之动态数组

目录


 1.摘要    

        数组是一种顺序存储的数据结构,在定义数组时,首先要确定数组的大小。静态数组在编译时就需要确定数组的大小,所以,为了防止内存溢出,我们尽量将数组定义的大一些,但是这样太过浪费内存。

     动态数组则不同,它不需要在编译时就确定大小,它的大小在程序运行过程中确定,所以可以根据程序需要而灵活的分配数组的大小,相比静态数组,它更“灵活”、“自由”。但是动态数组需要进行显式的内存释放

2.动态数组内存分配

1一维数组

int size = 10;              

//int size;

//cin>>size;         
 
int* Dynamic_Arr2 = new int[size];      //未初始化

        我们分配了一个动态数组,其实返回的是一个T类型的指针,指针指向的是数组的第一个元素,从这个角度来说,动态数组更像是指针。也是由于这种性质,导致了动态数组中size可以取0,即返回一个空指针,即分配一个空动态数组是合法的

2多维数组(以2维为例)


	int MAX_NUM = 10;
	int COL_NUM = 5, ROW_NUM = 3;
	double ***Arr3D = new double **[MAX_NUM];
 
	for (int i = 0; i < MAX_NUM; i++)
	{
		Arr3D[i] = new double *[ROW_NUM];
 
		for (int j = 0; j < ROW_NUM; j++)
		{
			Arr3D[i][j] = new double[COL_NUM];
		}
	}

3.动态数组初始化

1默认初始化

int* Dynamic_Arr3 = new int[size]();     //默认的初始化;

只在分配内存后加一对括号。

2.自定义初始化

string* Dynamic_Arr4 = new string[size]{"aa", "bb","cc", "dd", string(2, 'e') };      

4.动态数组释放

delete [ ] Dynamic_Arr4;

  delete释放数组是逆序进行的,最后一个元素被最先释放,第一个元素最后一个被释放。

      使用动态数组时,一定要记得显式的释放内存,否则很容易出错,比如在一个大的工程中,某一个for循环中或者某个函数中申请了内存却没释放,当函数不断地被调用,这些申请过的内存会一直堆积,直到最后退出程序。这很可能造成非常大的麻烦。
 

5.例子 Gradebook类的实现

实现了一个分数册,记录了(默认10个)学生的成绩和科目名称,实现了求均分,求分布等功能,这个类也是大学教程里比较重要的一个类,务必掌握。

#include<iostream>
#include<iomanip>
using namespace std;

class Gradebook {
public:
	const static int students = 10;
	Gradebook(string ,const int[]);
	void setCourseName(string);
	void processGrade();
	int getMax() const;
	int getMin()const;
	double getAverage();
	void outputChartBar();
	void outputGrades();
private:
	string courseName;
	int grades[students];
};

Gradebook::Gradebook(string s, const int a[]) {
	setCourseName(s);
	for (int i = 0; i < students; i++)
		grades[i] = a[i];
}

void Gradebook::setCourseName(string s) {
	courseName = s;
}

void Gradebook::processGrade() {
	outputGrades();
	cout << "Average of grades is : " << setprecision(2) << fixed << getAverage() << endl;
	cout << "The lowest is : " << getMin() << " The highest is : " << getMax() << endl;
	outputChartBar();
}

int Gradebook::getMax()const {
	int max = 0;
	for (int i = 0; i < students; i++)
		max = max > grades[i] ? max : grades[i];
	return max;
}

int Gradebook::getMin()const {
	int min = 101;
	for (int i = 0; i < students; i++)
		min = min < grades[i] ? min : grades[i];
	return min;
}

double Gradebook::getAverage() {
	int sum = 0;
	for (int i = 0 ; i < 10; i++)
		sum += grades[i];
	double average = (double)sum / students;
	return average;
}

void Gradebook::outputChartBar() {
	int* gradeDistribution = new int[students + 1] {0};
	for (int i = 0; i < students; i++) {
		gradeDistribution[grades[i] / 10]++;
	}
	cout << "The chartbar is showed below :" << endl;
	for (int count = 0; count < students + 1; count++) {
		if (count == 0)
			cout <<setw(6)<<right<< "0-9:";
		else if (count == 10)
			cout << setw(6) << right << "100:";
		else
			cout << count << "0-" << count << "9:";

		for (int stars = 0; stars < gradeDistribution[count]; stars++)
			cout << "*";
		cout << endl;
	}
}

void Gradebook::outputGrades() {
	for (int i = 0; i < students; i++)
		cout << "Students" << setw(2) << i + 1 << ":" << setw(3) << grades[i] << endl;
}

测试代码:

int main() {
	int grades[Gradebook::students] = { 99,100,60,88,76,87,67,59,69,93 };
	Gradebook mathsbook("maths", grades);
	mathsbook.processGrade();
}

 

6.参考文章

(4条消息) 【C++】细说C++中的数组之动态数组_c++动态数组_不用先生的博客-CSDN博客https://blog.csdn.net/u013921430/article/details/79601429

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2023年12月8日
下一篇 2023年12月8日

相关推荐