12.25 C++學習大綱:數組類型


C++學習大綱:數組類型

C++ 數組類型

數組類型是一種有固定多個同類型的元素按一定次序所構成的數據類型。

1. 一維數組

1)定義

[];

也可以藉助 typedef 類定義

typedef [];

2)操作

通過下標訪問元素。

注意下標是否越界。(C++中為了保證程序的執行效率不對下標越界進行檢查,越界時可以運行,但是結果不可預測)

初始化

int a[10] = {1,2,3};//其他元素初始化為0

int a[] = {1,2,3};//元素個數為3

2. 二維數組

1)定義

原理同一維數組

2)初始化

int a[2][3] = {{1,2,3},{4,5,6}}; 等同於 int a[2][3] = {1,2,3,4,5,6};//二維數組可以轉成一維數組進行處理,但是要注意下標

int a[][3] = {{1,2},{3,4,5}};//第一個下標可以省略,其他的不能,更高維的數組也同此。

按行存儲!

————————————————

C++學習大綱:數組類型


c++實現一個數組類

老師上課敲的例子,修改了一個bug

1 該數組類支持在對應位置刪除和插入元素

2 靜態對象只能調用靜態成員函數,因此[]符重載用了兩個版本

類頭文件

#pragma once

class MyVect

{

public:

MyVect();

MyVect(int n);

MyVect(const MyVect& other);//拷貝構造函數

MyVect& operator = (const MyVect& rhs); //重載賦值符

void push_back(double var);

double pop_back();

void insert(int n, double var);

void erase(int n);

void reallocate(); //重新分配內存

void clear();

void print();

double& operator[](int n);//重載[]符

const double& operator[](int n)const; //const成員函數,返回值是const

virtual ~MyVect();

private:

double* date; //數組指針

int date_size; //數組實際大小

int date_capacity; //數組容量

};


類源文件

#include "MyVect.h"

#include<iostream>

using namespace std;

MyVect::MyVect()

{

date = nullptr;

date_size = 0;

date_capacity = 0;

}

MyVect::MyVect(int n)

{

date_size = n;

date_capacity = 2 * n + 1;

date = new double[date_capacity];

}

double& MyVect::operator[](int n)

{

return date[n];

}

const double& MyVect::operator[](int n) const //常成員函數供常對象調用

{

return date[n];

}

MyVect& MyVect::operator=(const MyVect& rhs) //重載賦值符

{

if (this == &rhs)

{

return *this;

}

date_capacity = rhs.date_capacity;

date_size = rhs.date_size;

if (date != nullptr)

{

delete[] date;

}

date = new double[date_capacity];

for (int i = 0; i < rhs.date_size; i++)

{

date[i] = rhs.date[i];

}

return *this;

}

MyVect::MyVect(const MyVect& other)

{

this->date_size = other.date_size;

this->date_capacity = other.date_capacity;

date = new double[date_capacity];

for (int i = 0; i < date_size; i++)

{

date[i] = other.date[i];

}

}

void MyVect::reallocate() //重新分配內存

{

if (date_size == date_capacity)

{

double* old = date;

date_capacity = 2 * date_size + 1;

date = new double[date_capacity];

for (int i = 0; i < date_size; i++)

{

date[i] = old[i];

}

if (old != nullptr)

delete[] old;

}

}

void MyVect::push_back(double var)

{

this->reallocate();

date[date_size] = var;

date_size++;

}

double MyVect::pop_back()

{

double out = date[date_size-1];

date_size--;

return out;

}

void MyVect::insert(int n, double var)

{

reallocate();

for (int i = date_size; i >n; i--)

{

date[i] = date[i-1];

}

date[n] = var;

date_size++;

}

void MyVect::erase(int n)

{

for (int i = n; i < date_size; i++)

{

date[i] = date[i + 1];

}

date_size--;

}

void MyVect::clear()

{

date_size = 0;

date_capacity = 0;

if (date != nullptr)

delete [] date;

date = nullptr; //此句不可忘,否則析構時刪除野指針出現內存錯誤

}

void MyVect::print()

{

for (int i = 0; i < date_size; i++)

cout << date[i] << '\\t';

cout << endl;

}

MyVect::~MyVect()

{

date_size = 0;

date_capacity = 0;

if (date!=nullptr)

{

delete[] date;

}

date = nullptr;

//cout << "析構啦!" << endl;

}

————————————————

C++學習大綱:數組類型

通過分享實用的計算機編程語言乾貨,推動中國編程到2025年基本實現普及化,使編程變得全民皆知,最終實現中國編程之崛起,這裡是中國編程2025,感謝大家的支持。

原文鏈接:https://blog.csdn.net/haitaolang/article/details/70017461


分享到:


相關文章: