数组,结构体与类
数组的声明:
int list[5];
数组初始化时可以一次性赋值;如果不在初始化时一次性赋值,只能一个一个赋值了; #includeusing namespace std; int main() { int a[10]= {1,2,3,4,5,6,7,8,9,0}; for (int i = 0; i < 10; ++i) { cout << a[i] < 陷阱:数组的索引始终是从0开始的,同一数组的元素类型是一样的。
陷阱:数组越界导致程序错误;c++中数组也是可以作为形参的,而c语言中则是不可以的;
#includeusing namespace std; void test(int a[],int size); int main() { int a[10]= {1,2,3,4,5,6,7,8,9,0}; test(a,10); return 0; } void test(int a[],int size) { for (int i = 0; i < size; ++i) { cout << a[i] < 数组参数与引用参数相似,在函数中改变数组形参也会影响到函数外的数组;
在使用数组参数是,一般还要传出一个int型变量来存储数组的大小fd#includeusing namespace std; void test(int a[],int size); int main() { int a[10]= {1,2,3,4,5,6,7,8,9,0}; test(a,10); for (int i = 0; i < 10; ++i) { cout << a[i] << endl; } return 0; } void test(int a[],int size) { for (int i = 0; i < size; ++i) { a[i] =a[i] + 1; } } 数组的查询
#includeusing namespace std; const int mun = 20; void fill(int a[],int size,int &number_new); void search(int a[],int number_new,int b); void check(int &b); int main() { int new_number,a[mun],b; fill(a,mun,new_number); cout << new_number << endl; for (int i = 0; i < new_number; ++i) { cout << a[i] << endl; } cout << "请输入你所要搜素的数字:"; cin >> b; search(a,new_number,b); return 0; } void fill(int a[],int size,int &number_new) { int count,b,c; cout << "请输入20个数字" << endl << "当你向停止输入时,请输入一个负数" << endl; do { cin >> b; a[count] = b; count ++; }while((count < size)&&(b > 0)); number_new = count; } void search(int a[],int number_new,int b) { for (int i = 0; i < number_new; ++i) { if(a[i] == b) { cout << "寻找到目标:" << a[i] << endl; return; } } cout << "很遗憾,我们没有找到你所需要的目标." << endl; } 测试,通过测试发现当你输入多余的输入项,会占用之后的输入项;
#includeusing namespace std; const int mun = 2; void test(int a[],int size); int main() { int a[mun],b; test(a,mun); cin >> b; cout << b; return 0; } void test(int a[],int size) { int count,b; do { cin >> b; a[count] = b; count ++; cout << "q" << endl; } while(count < size && b > 0); } 排序:
使用冒泡排序时要记住:冒泡排序是需要嵌套循环的:#includeusing namespace std; const int mun = 20; void fill(int a[],int size,int &number_new); void sort(int a[],int &number_new); int main() { int new_number,a[mun],b; fill(a,mun,new_number); cout << new_number << endl; for (int i = 0; i < new_number; ++i) { cout << a[i] << endl; } sort(a,new_number); for (int i = 0; i < new_number; ++i) { cout << a[i]<< endl; } return 0; } void fill(int a[],int size,int &number_new) { int count,b,c; cout << "请输入20个数字" << endl << "当你向停止输入时,请输入一个负数" << endl; cin >> b; do { a[count] = b; count ++; cin >> b; }while((count < size)&&(b > 0)); number_new = count; } void sort(int a[],int &number_new) { int b,temp; cout << "请选择排序方式:n升序 ————1n降序————2"; cin >> b; if(b == 1) { for (int i = 0; i < number_new -1; ++i) { for (int i = 0; i < number_new - 1; ++i) { if (a[i] > a[i + 1]) { temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } else { ; } } } } else if(b == 2) { for (int i = 0; i < number_new -1; ++i) { for (int i = 0; i < number_new - 1; ++i) { if (a[i] < a[i + 1]) { temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } else { ; } } } } else { cout << "输入错误,请重新输入。n"; } } 选择排列
#includeusing namespace std; const int mun = 20; void fill(int a[],int size,int &number_new); void sort(int a[],int &number_new); int main() { int new_number,a[mun],b; fill(a,mun,new_number); cout << new_number << endl; for (int i = 0; i < new_number; ++i) { cout << a[i] << endl; } sort(a,new_number); for (int i = 0; i < new_number; ++i) { cout << a[i]<< endl; } return 0; } void fill(int a[],int size,int &number_new) { int count,b,c; cout << "请输入20个数字" << endl << "当你向停止输入时,请输入一个负数" << endl; cin >> b; do { a[count] = b; count ++; cin >> b; }while((count < size)&&(b > 0)); number_new = count; } void sort(int a[],int &number_new) { int b,temp,pos; cout << "请选择排序方式:n升序 ————1n降序————2"; cin >> b; if(b == 1) { for (int i = 0; i < number_new -1; ++i) { pos = 0; for (int i = 0; i < number_new - 1 -i; ++i) { if (a[pos] < a[i + 1]) { pos = i + 1; } if (pos != number_new-1) { temp = a[pos]; a[pos] = a[number_new -1 -i]; a[number_new -1 -i] = temp; } } } } else if(b == 2) { for (int i = 0; i < number_new -1; ++i) { for (int i = 0; i < number_new - 1; ++i) { if (a[i] < a[i + 1]) { temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } else { ; } } } } else { cout << "输入错误,请重新输入。n"; } } 多维数组
赋值与显示:#include#include using namespace std; int main() { int a[30][100]; srand(90); for (int i = 0; i < 30; ++i) { for (int j = 0; j < 100; ++j) { a[i][j] = rand()/1001; } } for (int i = 0; i < 30; ++i) { cout << endl; for (int j = 0; j < 100; ++j) { cout << a[i][j] <<"t"; } } return 0; } 结构体:
结构体可以看作没有任何函数的对象;
类则是包含函数与变量的对象;#includeusing namespace std; struct students { int id; short sex; char age; }; int main() { struct students st = {12,1,'q'}; cout << st.age << endl << st.sex << endl < 复合结构体
#includeusing namespace std; struct students { int id; short sex; char age; }; struct people { int name; struct students st1; }; int main() { struct people st = {12,1,2,'q'}; cout << st.name << endl << st.st1.id << endl < 类
#includeusing namespace std; class test { public: void show(); int test_a; int test_b; }; int main() { class test ts1; ts1.test_a = 1; ts1.test_b = 2; cout << ts1.test_a <<"t" < "::"被称为作用域运算符;他前面的类名被成为类型限定语
点运算符与作用域运算符都是用于表明成员所属于那个对象或类抽线数据类型:数据类型的值以及相关操作对程序员不可见
通过类隐藏成员函数或数据,称为信息隐藏,数据抽象,封装公有(public)成员与私有(private)成员:
类一旦私有化,私有成员变量只能在成员函数的定义中,其他都是非法的#includeusing namespace std; class test { public: int show(); void set_a(); void set_b(); private: int a; int b; }; int main() { class test ts; ts.set_a(); ts.set_b(); cout << ts.show(); return 0; } int test::show() { return a+b; } void test::set_a() { cout << "请输入一个加数,a:n"; cin >> a; } void test::set_b() { cout << "请输入一个加数,b:n"; cin >> b; }



