c new字串的一點心得

2021-04-21 14:59:15 字數 920 閱讀 3038

c++中,new的用法很靈活,這裡進行了簡單的總結: 1. new() 分配這種型別的乙個大小的記憶體空間,並以括號中的值來初始化這個變數; 2. new 分配這種型別的n個大小的記憶體空間,並用預設建構函式來初始化這些變數; #include

#include

using namespace std; int main() { //char* p=new char("hello"); //error分配乙個char(1位元組)的空間, //用"hello"來初始化,這明顯不對 char* p=new char[6]; //p="hello"; //不能將字串直接賦值給該字元指標p,原因是: //指標p指向的是字串的第乙個字元,只能用下面的 //strcpy strcpy(p,"hello"); cout<<*p<

<

<

#include

using namespace std; int main() { int *a = new int[34]; int *b = new int; int (*c)[2] = new int[34][2]; int (*d)[2] = new int[2]; int (*e)[2][3] = new int[34][2][3]; int (*f)[2][3] = new int[2][3]; a[0] = 1; // b[0] = 1; //執行時錯誤,無分配的記憶體,b只起指標的作用,用來指向相應的資料 c[0][0] = 1; // d[0][0] = 1;//執行時錯誤,無分配的記憶體,d只起指標的作用,用來指向相應的資料 e[0][0][0] = 1; // f[0][0][0] = 1;//執行時錯誤,無分配的記憶體,f只起指標的作用,用來指向相應的資料 cout<

<

<

<

<

<

<

<

<

<

<

<

SQL一點心得

sql語句將所有 stock 表裡的 縮寫 led甲 替換改寫成 led刷 update dbo stock set 縮寫 replace 縮寫 led甲 led刷 where 縮寫 like led甲 go字首 update mytable set myfield replace myfield,...

Cell myCell一點心得

ctor initializer形如 cell cell mvalue 0 mstring ctor initializer,能在建立資料成員的同時賦初值 1.const方法的工作原理是將方法內用到的資料成員都標記為const引用。因此試圖修改資料成員時,編譯器報錯。2.用explicit關鍵字標記...

openjudge 一點心得

031 校門外的樹 總時間限制 1000ms 記憶體限制 65536kb 描述某校大門外長度為l的馬路上有一排樹,每兩棵相鄰的樹之間的間隔都是1公尺。我們可以把馬路看成乙個數軸,馬路的一端在數軸0的位置,另一端在l的位置 數軸上的每個整數點,即0,1,2,l,都種有一棵樹。由於馬路上有一些區域要用來...