数组相当于一个长度固定的 vector 类型;指针类似 iterator.数组和指针类似于 vector 和 iterator 的低级复合类型。
同 iterator 对于 vector 一样,指针可以指向数组存储空间的下一个单元。
一、数组:类型名,标示符,维数
维数:常量表达式。只含整形字面值常量,枚举常量,常量表达式初始化的整型 const 对象。数组长度:size_t.
数组输出:
const size_t arr_sz=5;
int arr[arr_sz]={0,1,2,3,4};
for(int *pbegin=arr,*pend=arr+arr_sz;pbegin!=pend;++pbegin)
cout<<*pbegin<<" ";
数组初始化 vector 对象:
const size_t arr_sz=6;
int int_arr[arr_sz]={0,1,2,3,4,5};
vector<int> ivec(int_arr,int_arr+arr_sz); // vector:0,1,2,3,4,5
vector<int> ivec2(int_arr+1,int_arr+4); // vector:1,2,3
动态数组:每个程序执行都占有一块可用内存空间存放动态分配对象,此内存空间称为程序的自由存储区(或堆)。
初始化:
int *pia=new int[10]; // 内置类型未初始化。返回第一个元素指针,new 类似于 malloc
string *ps=new string[10]; // 10 empty strings, initialized :unique
int *pia2=new int[10](); // initialized to 0. :unique
const 对象的动态数组必须初始化。
char arr[0]; // error
char *cp=new char[0]; //ok ,返回有效的非零指针(不能解引用)。允许比较运算,+(-)0,减本身得0
释放数组空间:delete [] pia; // 无[],会产生内存泄露
多维数组:
多维数组指针:
/********* 区别:*p[n] 和 (*p)[n] **********/
int ia[3][4]={{1,2,3,4},{5,6,7,8},{9}};
int *p[4]; // 指针数组
int (*p2)[4]=ia; // ip是指向含有4个元素的数组的指针
cout<<**p2<<endl; // <==> ip[0][0]
cout<<p2[1][2]<<endl;
用 typedef 类型定义:
/*************************************
typedef 定义方式:
typedef int int_array[4];
int_array *ip=ia;
*************************************/
int ia[3][4]={{1,2,3,4},{5,6,7,8},{9}};
typedef int int_array[4];
for(int_array *p=ia;p!=ia+3;++p) // p 指向 ia 第一个内部数组
for(int *q=*p;q!=*p+4;++q) // q = *p,获得有四个 int 型元素数组,返回第一个元素指针
cout<<*q<<" ";
二、指针:
初始化和赋值:除了类型匹配的对象地址和指针外,还有:0,整形 const 对象,预处理器变量 NULL (cstdlib 头文件中定义,了解)// 等于初始化为0。
特殊指针类型 void*:可赋值为任何类型指针,可保存任何类型对象地址,但不允许操纵所指对象。
有限操作:1,与另一指针比较。 2,向函数传递或者由函数返回。3,给另 void* 指针赋值。
指针相减的结果是标准库类型 ptrdiff_t 的数据。(cstddef 中定义,了解)
下标和指针:下标是指针的下标,下标操作时对指针的操作。数组名是指向数组首元素的指针。
int ia[10]={1,3,5};
int *p=&ia[1];
int j=p[1]; // j=a[2]=5
int k=p[-1]; // k=a[0]=-1
指针与 const 限定符:
add:数据类型分为:const 和 非 const.其类型特性与指针类型特性无关。
但const对象只能用 const对象指针。
1.const 对象指针:(const double *p) :能赋对象地址,不能修改对象值(编译错误)。自认为指向const对象。
const double pi=3.14; A
double *ptr=π // error
const double *cptr=π // ok B
*注意 A 和 B 的区别。
double pi=3.24;
const double *ptr=π // ok
&pi=2; // error
2.const 指针:(double *const p):自身的值不能修改(编译错误),但可以用来给指定的对象空间赋值。
int errNumb=0;
int *const curErr=&errNumb; // const 量必须初始化
*curErr=3;
cout<<errNumb; // 输出 3
3.const 对象的 const 指针 :(const double *const p)
const double pi=3.14;
const double *const pi_ptr=π
4.指针 和 typedef
typedef string *pstring;
const pstring cstr; // <==> string *const cstr;
<=/=>const string *cstr;
string s;
typedef string *pstring;
const pstring cstr1=&s; pstring const cstr2=&s; string *const cstr3=&s; 三个等价。
Append:字符串:
C 风格字符串与 C++ 的标准库类型 string 比较:
const char *pc = "a very long literal string"; // implementation
const size_t len = strlen(pc);
for(size_t ix=0;ix<1000000;++ix)
{
char *pc2 = new char[len+1];
strcpy(pc2,pc);
if(strcmp(pc2,pc)) // test,then use the new string
;
delete[]pc2; // free the memory
}
/***********************************************************************/
string str("a very long literal string"); //implementation
for(int ix=0;ix!=1000000;++ix)
{
string str2=str; // do copy, automatically allocated
if(str2!=str) // test,then use the new string
;
} // str2 is automatically freed
转换:
C 风格字符串可直接对 string 对象进行初始化或赋值;
string 对 C 风格字符串初始化或赋值:
string st2("Hello Wold!");
const char *str=str2.c_str(); // c_str()返回的指针指向 const char 类型的数组