当前位置:网站首页>STL tutorial 2-myarray framework implementation

STL tutorial 2-myarray framework implementation

2022-06-21 08:31:00 Sleepy snail

Can store any type of data , Implement initialization ,= Symbol overloading , Right quoting , Memory free

1、 Basic framework

1、 Class member variable
Capacity int mCapacity;
Existing elements int mSize;
The first address of the data int *pAddr;
2、 Class function

MyArray(int capacity)// Realization array initialization 

MyArray()// Implement class initialization 

T& operator[](int index)// Returns an array of specified elements   such as arr[3]

MyArray<T> operator=(const MyArray<T>& arr)//MyArray<int> arr2(10); arr2 = arr;

void PushBack(T& data)//  Add elements to it , This method can only add lvalues  a=3 arr.PushBack(a);

void PushBack(T&& data) //  Add the right value to it  , This method can realize arr.PushBack(3);

~MyArray();  // Destructor , Free space for classes 

stay class Add template< class T>

2、MyArray(int capacity)

MyArray(int capacity)
	{
    
		this->mCapacity = capacity;
		this->mSize = 0;
		// Application memory 
		this->pAddr = new T[this->mCapacity];
	}

3、MyArray(const MyArray& arr)

MyArray(const MyArray<T>& arr) {
    
		this->mCapacity = arr.mCapacity;
		this->mSize = arr.mSize;
		// Application memory 
		this->pAddr = new T[this->mCapacity];
		for (int i = 0; i < mSize; i++) {
    
			pAddr[i] = arr.pAddr[i];
		}
	}

4、T& operator[](int index)

T& operator[](int index) {
    
		//pAddr It's a T An array of types , What is returned here is to specify... In the array index The element of location 
		return this->pAddr[index];
	}

5、MyArray operator=(const MyArray& arr)

	MyArray<T> operator=(const MyArray<T>& arr) {
    
		// If you have already applied for space , Then free up space 
		if (this->pAddr != NULL) {
    
			delete[]pAddr;
		}
		this->mCapacity = arr.mCapacity;
		this->mSize = arr.mSize;
		// Application memory 
		this->pAddr = new T[this->mCapacity];
		for (int i = 0; i < mSize; i++) {
    
			pAddr[i] = arr.pAddr[i];
		}
		// return class The pointer to 
		return *this;
	}

6、void PushBack(T&& data)

	void PushBack(T&& data) {
    
		if (mSize >= mCapacity)return 0;
		// Be careful msize It's from 1 At the beginning 
		pAddr[mSize] = data;
		mSize++;

	}

test

	MyArray<int> arr(10);
	int a = 10, b = 20;
	arr.PushBack(a);
	arr.PushBack(b);

Complete code

#include <iostream>

using namespace std;

template<class T>
class MyArray {
    

public:
	// Capacity 
	int mCapacity;
	// Existing elements 
	int mSize;
	// The first address of the data 
	int *pAddr;

public :
	MyArray(int capacity)
	{
    
		this->mCapacity = capacity;
		this->mSize = 0;
		// Application memory 
		this->pAddr = new T[this->mCapacity];
	
	}

	MyArray(const MyArray<T>& arr) {
    
		this->mCapacity = arr.mCapacity;
		this->mSize = arr.mSize;
		// Application memory 
		this->pAddr = new T[this->mCapacity];
		for (int i = 0; i < mSize; i++) {
    
			pAddr[i] = arr.pAddr[i];
		}
	
	}
	T& operator[](int index) {
    
		//pAddr It's a T An array of types , What is returned here is to specify... In the array index The element of location 
		return this->pAddr[index];
	}

	//= Symbol overloading 
	MyArray<T> operator=(const MyArray<T>& arr) {
    
		// If you have already applied for space , Then free up space 
		if (this->pAddr != NULL) {
    
			delete[]pAddr;
		}
		this->mCapacity = arr.mCapacity;
		this->mSize = arr.mSize;
		// Application memory 
		this->pAddr = new T[this->mCapacity];
		for (int i = 0; i < mSize; i++) {
    
			pAddr[i] = arr.pAddr[i];
		}
		// return class The pointer to 
		return *this;
	}
	void PushBack(T& data) {
    
		if (mSize >= mCapacity)return ;
		// Be careful msize It's from 1 At the beginning 
		//1、 Object elements must be able to copy 
		//2、 Containers are all value semantics , Not reference semantics , Putting an element into a container is a copy of the element , That is to say, they are all copies 
		//3、 If a member of an element has a pointer , Pay attention to deep and shallow copies 
		    //3.1  Deep copy and light copy 
		this->pAddr[this->mSize] = data;
		this->mSize++;
	}

	~MyArray() {
    
		if (this->pAddr != NULL) {
    
			delete[] this->pAddr;
		}
	}
    // Take a reference to the right value 
	void PushBack(T&& data) {
    
		if (mSize >= mCapacity)return 0;
		// Be careful msize It's from 1 At the beginning 
		pAddr[mSize] = data;
		mSize++;
	}
};
void test01()
{
    
	MyArray<int> arr(10);
	int a = 10, b = 20;
	arr.PushBack(a);
	arr.PushBack(b);
	//  You can't arr.PushBack(2); You cannot take a reference to a right value 
    /*  Lvalues can be used on multiple lines, such as a=10; *  The right value is only a temporary variable , Can only be used on the current line  */
}
class Person {
    };
void test02() {
    
	Person p1, p2;
	MyArray<Person> arr2(10);
	arr2.PushBack(p1);
	arr2.PushBack(p2);
}
int main() {
    
	test01();
	return 0;	
}

7、 Left and right

int a = 10, b = 20;
arr.PushBack(a);
arr.PushBack(b);
	void PushBack(T& data) {
    
		if (mSize >= mCapacity)return ;
		// Be careful msize It's from 1 At the beginning 
		//1、 Object elements must be able to copy 
		//2、 Containers are all value semantics , Not reference semantics , Putting an element into a container is a copy of the element , That is to say, they are all copies 
		//3、 If a member of an element has a pointer , Pay attention to deep and shallow copies 
		    //3.1  Deep copy and light copy 
		this->pAddr[this->mSize] = data;
		this->mSize++;
	}

The above code can only pass in variables, that is, only one lvalue , The right value will only take effect in the current line , So if you want to pass in the right value , Need to see c++11 Newly introduced

	void PushBack(T&& data) {
    
		if (mSize >= mCapacity)return 0;
		// Be careful msize It's from 1 At the beginning 
		pAddr[mSize] = data;
		mSize++;

	}

8、 Containers are all value semantics

Containers are all value semantics , Not reference semantics , Putting an element into a container is a copy of the element , That is to say, they are all copies
If a member of an element has a pointer , Pay attention to deep and shallow copies

9、 Deep copy and light copy

If there's a pointer , Is the address pointed to by the pointer a copy

原网站

版权声明
本文为[Sleepy snail]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206210823009038.html