C++:动态数组类 动态创建多维数组 vector深复制

动态数组类
如果我们的类中需要使用动态申请的内存空间,并且该空间是依附于对象的,我们一般在该类的构造函数中去申请空间(new),在该类的析构函数中释放空间(delete).

#include
#include
using namespace std;

class Point{
public:
	Point():x(0),y(0){
		cout<<"Default Constructor called."<=0 && index>count;
	ArrayOfPoints points(count);
	points.element(0).move(5,10);//通过访问数组元素的成员
	points.element(1).move(15,20);//通过类访问数组元素的成员 
	return 0; 
}

动态申请多维数组(难点)
int *p=new int [6];
可以申请一个二维数组吗?
int *p=new int [2][3];
int *p=new int[2];
//int *p=new int [2][3];
在于他们返回的指针类型是不同的
int(*p)[3]=new int[2][3];
p=new int[size][3];
只有 第一维的容量可以使用变量,其他维度必须使用常量.

#include
using namespace std;
int main(){
	//int *p=new int[2];
	//int *p=new int[2][3];
	int (*p)[3];
	int size=5;
	p=new int[size][3];
	//*(*(p+i)+j)
	//p[i][j]
	
	delete[]p;
} 
#include
using namespace std;
int main(){
	float(*cp)[9][8]=new float[8][9][8];
	for(int i=0;i<8;i++)
		for(int j=0;j<8;j++)
			for(int k=0;k<8;k++)
				//以指针形式数组元素
				*(*(*(cp+i)+j)+k)=static cast(i*100+j*10+k);
	for(int i=0;i<8;i++){
		for(int j=0;j<9;j++){
			for(int k=0;k<8;k++)
				//将指针cp作为数组名使用,通过数组名和下标访问数组元素
				cout<

vector简介
C++标准库中封装好的一个动态数组类
大致的用法:

#include
#include
using namespace std;
int main(){
	int b[]={3,4,5};
	vectora(b,b+3);
	cout<
#include
#include
using namespace std;
//计算数组arr中元素的平均值
double average(const vector&arr){
	double sum=0;
	for(unsigned i=0;i>arr[i];//运算符重载
		cout<<"Average="<

深复制与浅复制(重点)
当类的成员中含有指针数据成员的时候,通过复制构造来创建新的对象可能会遇到问题.为什么?使用默认的复制构造函数,完成的是无修改的复制,会导致不同对象的指针成员指向同一块内存空间(往往和实际需求不符),这种复制,我们称其为浅复制.
如何解决这个问题?
使用深复制,自定义复制构造函数,控制复制构造的过程.

#include
#include
using namespace std;

class Point{
public:
	Point():x(0),y(0){
		cout<<"Point"<=0 && index>count;
	ArrayOfPoints points1(count);//创建对象数组
	points1.element(0).move(5,10);//通过访问数组元素的成员
	points1.element(1).move(15,20);//通过类访问数组元素的成员
	
	ArrayOfPoints points2(points2);
	return 0; 
} 
#include
#include
using namespace std;
class Point{
public:
	Point():x(0),y(0){
		cout<<"Default Constructor called."<=0 && index>"Please enter the count of points:";
	cin>>count;
	ArrayOfPoints pointsArray1(count);//创建对象数组
	pointsArray1.element(0).move(5,10);
	pointsArray1.element(1).move(15,20);
	ArrayOfPoints pointsArray2=pointsArray1;//创建对象数组副本
	cout<<"Copy of pointsArray1:"<

#include
#include
using namespace std;
class Point{
public:
Point():x(0),y(0){
cout<<“Default Constructor called.”< }
Point(int x,int y):x(x),y(y){
cout<<“Constructor called.”< }
~Point(){cout<<“Destructor called.”< int getX()const{return x;}
int getY()const{return y;}
voidmove(int newX,int newY){
x=newX;
y=newY;
}
private:
int x,y;
};
//动态数组类
class ArrayOfPoints{
public:
ArrayOfPoints(int size):size(size){
points=new Point[size];
}
ArrayOfPoints(const ArrayOfPoints&v);
~ArrayOfPoints(){
cout<<“Deleting…”< delete[]points;
}
//获得下标为index的数组元素
Point &element(int index){
assert(int index>=0 && index return points[index];
}
private:
Point *points;//指向动态数组首地址
int size;//数组大小
};
ArrayOfPoints::ArrayOfPoints(const ArrayOfPoints&v){
size=v.size;
points=new Point[size];
for(int i=0;i points[i]=v.points[i];
}
int main(){
int count;
cout<<“Please enter the count of points:”;
cin>>count;
ArrayOfPoints pointsArray1(count);//创建对象数组
pointsArray1.element(0).move(5,10);
pointsArray1.element(1).move(15,20);
ArrayOfPoints pointsArray2=pointsArray1;//创建对象数组副本
cout << “Copy of pointsArray1:” << endl;
cout << "Point_0 of array2: " << pointsArray2.element(0).getX() << ", "
<< pointsArray2.element(0).getY() << endl;
cout << "Point_1 of array2: " << pointsArray2.element(1).getX() << ", "
<< pointsArray2.element(1).getY() << endl;

pointsArray1.element(0).move(25, 30);
pointsArray1.element(1).move(35, 40);
cout << "After the moving of pointsArray1:" << endl;
cout << "Point_0 of array2: " << pointsArray2.element(0).getX() << ", "
	<< pointsArray2.element(0).getY() << endl;
cout << "Point_1 of array2: " << pointsArray2.element(1).getX() << ", "
	<< pointsArray2.element(1).getY() << endl;
retrun 0;

}
动态数组类:简单版

#include
#include
using namespace std;
class Point{
public:
	Point():x(0),y(0){
		cout<<"Default Constructor called."<=0 && index>count;
	ArrayOfPoints points(count);//创建对象数组
	points.element(0).move(5,10);//通过访问数组元素的元素
	points.element(1).move(15,20);//通过类访问数组元素的元素
	return 0; 
} 

你可能感兴趣的:(C++)