当前位置:网站首页>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
边栏推荐
- Eureka's timedsupersortask class (periodic task with automatic interval adjustment)
- 2022-2028 global postoperative pressure suit industry research and trend analysis report
- Visual studio code annotation plug-in: korofileheader
- 2022-2028 global boom cylinder industry research and trend analysis report
- MySQL filter query (start with a letter, start with a number, start without a number, and start without a letter)
- 2022-2028 global internal gear motor industry research and trend analysis report
- Global and Chinese market for online automatic optical inspection 2022-2028: Research Report on technology, participants, trends, market size and share
- 4.7 Inquirer. JS usage example
- Higher order functions in kotlin (first-class citizens)
- 积分签到任务设置的要求,积分商城搭建过程中常见的问题
猜你喜欢

2022-2028 global boom cylinder industry research and trend analysis report

移动应用开发总结

5 minutes to understand MySQL - row to column

Gql+nodejs+mysql database
![[actual combat] ACM players illustrate leetcode using stack to realize queue](/img/f7/0a21f2fdc7c18f352c1b134d27c21c.jpg)
[actual combat] ACM players illustrate leetcode using stack to realize queue

Using elastic stack to analyze Olympic data (II)

Antd table how scroll bars appear in long tables

Decrypt FTP

Client construction and Optimization Practice

日記(C語言總結)
随机推荐
4.5 dataset usage document
Eureka的TimedSupervisorTask类(自动调节间隔的周期性任务)
4.7 Inquirer. JS usage example
2022-2028 global section valve industry research and trend analysis report
WordPress media library supports uploading and previewing SVG icons
Represent each record in the dataframe as a dictionary
【MGT】代码解读之model-MGT
Windows10 LAN shared folder process
sql查看数据库/表磁盘占用情况,杀死进程终止tidb中的连接
Unity 5 自帶的Mono也可以支持C# 6
关于sql的问题:两张表的字段关联问题
Unity .net 框架问题
Vision_ Transformer code exercise
[kotlin] first day
MySQL filter query (start with a letter, start with a number, start without a number, and start without a letter)
Tidb and MySQL modify system variables / common statements (kill the process in process)
Eureka's timedsupersortask class (periodic task with automatic interval adjustment)
Summary of problems and errors encountered in tidb4.0.0 (tiup deployment)
2022-2028 global cooling on-off valve industry research and trend analysis report
Use lua+redis+openresty to realize concurrent optimization of e-commerce Homepage