c++学习-数组
int a[10]; //是个元素,在windows下回报错,linux会输出一个随机数
int a[10]={1,2}; //初始化,其他的为0
数组越界:
为了调高效率, 编译器不会对数组越界做检查
#include <iostream> using namespace std; int main() { int b[10]; int a[2]; b[10] = 3; cout<<b[10]<<endl; cout<<a[0]<<endl; return 0; }
int a[10]; // 自动生成一个指针,该指针指向数组的第一个元素的地址 int *a=&a[0];
数组的传递:
void test(int []);//简要数组声明
void test(int [10]);//标准数组声明
void test(int *a);//指针声明
数组对象:
#include<iostream> using namespace std; class area{ public: area(){} area(int w, int h){ this->w = w; this->h = h; cout << "gouzao" << endl; } ~area(){ cout << "xigou" << endl; } int get() { return w*h; } private: int w; int h; }; int main() { area a[5] = {area(1,2),area(3,4)}; cout << a[0].get() << endl; return 0; }
指针数组:
#include<iostream> using namespace std; class area{ public: area(){ cout << "gouzao:" <<endl; } area(int i){ this->i = i; cout << "gouzao:" << i << endl; } area(int w, int h){ this->w = w; this->h = h; cout << "gouzao" << endl; } ~area(){ cout << "xigou "<<i << endl; } int get() { return w*h; } private: int w; int h; int i; }; int main() { area *p[5];//指针数组 int i; for (i = 0; i < 5; ++i) { p[i] = new area(i); } //释放内存 for (i = 0; i < 5; ++i) { delete p[i]; } return 0; }
优化后:
#include<iostream> using namespace std; class area{ public: area(){ cout << "gouzao:" <<endl; } area(int i){ this->i = i; cout << "gouzao:" << i << endl; } area(int w, int h){ this->w = w; this->h = h; cout << "gouzao" << endl; } ~area(){ cout << "xigou "<<i << endl; } int get() { return w*h; } void set(int w, int h) { this->w = w; this->h = h; } private: int w; int h; int i; }; int main() { area *p = new area[5]; //在堆中创建连续的五个对象,并将第一个对象的地址赋值给指针 p int i; for (i = 0; i < 5; i++) { p[i].set(i,i); } delete []p; //特有的删除方式 return 0; }
枚举常量和数组:
int main() { enum day{mon, tue, wen, thur ,fri,sat, sun}; double tempature[sun + 1] = { 5, 3, 4, 5, 5, 5, 6}; int i; for (i = 0; i <= sun; i++) { cout << "xingqi " << i << "\t" << "qiwen:" << tempature[i] << endl; } }
有 \0 直接输出数组名即可
int main() { char c[] = {'a','b', '\0'}; //cout << sizeof(c) << endl; cout << c << endl; return 0; }
int main() { char c[2]; //cin >> c; //不检查是否越界,遇到空格会结束 //gets(c); //不检查是否越界,会接收所有的字符 cin.get(c,11);//解决上面问题 cout << sizeof(c) << endl; //cout << (int)c[2] << endl; //cout << (int)c[0] << endl; return 0; }
strcat
int main() { char a[10] = "ab";//注意第一个要足够的大 char b[] = "cd"; strcat(a, b); //连接时第一个字符串末尾的 '\0' 会自动去掉 cout << a << endl; cout << sizeof(a) << endl; return 0; }
strcpy
int main() { char a[10] = "ab"; char b[] = "cd"; strcpy(a,b); //b可以是一个字符串,注意a要足够大 //strcpy(a, "def"); cout << a << endl; cout << sizeof(a) << endl; return 0; }
strcmp
int main() { char a[10] = "ab"; char b[] = "ab"; if (strcmp(a, b) == 0) // strcmp("a", "b") { cout << "equal"<<endl; } return 0; }
重载[]运算符解决数组越界问题:
class A{ public: A(int len){ this->len = len; p = new char[len]; } ~A(){ delete[]p; p = 0; } char & operator[](int i) { if (i<0 || i>=len) { cout << "越界" << endl; return *(p + len - 1); } else{ return *(p + i); } } private: int len; char *p; }; int main() { A a(5); int i; char *p = "abcdefghkjklmn"; for (i = 0; i < 10; ++i) { a[i] = *(p + i); } for (int j = 0; j < 5; j++) { cout << a[j] << endl; } return 0; }