c 學習 基礎 動態陣列

2021-10-18 03:57:28 字數 1593 閱讀 9391

//建立動態陣列

#include

#include

using

namespace std;

intmain()

int*pia =

newint

[n];

if(pia !=

null

)//釋放

free

(p);

delete

pia;

return0;

}

//建立動態陣列

#include

#include

using

namespace std;

class

dog;

intmain()

;int

*pia =

newint[10

];//沒有初始化

string *psa =

new string[10]

;//預設的建構函式初始化

dog *pdog =

new dog[10]

;//同樣是預設的建構函式初始化

int*pia2 =

newint[10

]();

//初始化

cout<<

*pia2<*pia =9;

*(pia+1)

=8;*

(pia2+3)

=6; cout <<

*(pia+1)

*(pia2+3)

int*q=pia;q!=pia+

10;q++

)for

(int

*q=pia;q!=pia+

10;q++

)const

int*pci_bad =

newconst

int[

100]()

;char arr[0]

;//

char

*cp =

newchar[0

];cout<<

"請輸入動態陣列大小:"

cin>>n;

int*p =

newint

[n];

for(

int*q=p;q!=p+n;

++q)

delete

pci_bad;

//不要忘記

delete

cp;delete

p;return0;

}

#include

#include

using

namespace std;

intmain()

string str

("a very long literal string");

for(

int ix =

0;ix !=

100;

++ix)

return0;

}

c 基礎學習之動態陣列

最近新到公司,帶教老師讓我寫動態陣列,哇,給我的感覺真的是,底層的東西,要求效能和 規範比較嚴,下面是 我會寫很詳細的注釋,方便以後自己 希望各位請教了。myarray.h ifndef myarray h define myarray h include 函式後面加const表示函式不改變類中的成...

c 動態陣列學習筆記

1.動態陣列 之所以出現動態陣列,是由於靜態陣列的大小是固定的。當我們一開始並不知道需要多大的陣列時,靜態陣列就無法使用了。2.操作 動態陣列是使用指標來承載malloc 或new操作符動態分配的記憶體空間,然後在需要的更新陣列大小的時候釋放空間使用free 或delete。3.使用malloc 和...

C 基礎學習(三)陣列

關於陣列的語法點還是很多的,基本陣列,字元型陣列,指標陣列,還有c 標準庫中的關於陣列的容器,今天只簡單總結下,關於c 中陣列的宣告和使用的一些細節。includeusing namespace std void main 常用的方式 int myarray2 4 可以不設定初值,就預設陣列中全為0...