cpp遍历数组的几种方式

来源:互联网 发布:买域名是啥 编辑:程序博客网 时间:2024/06/04 19:13

int ia[] = { 0,2,3,4,5,6,7,8,9 }; int ia_sz = sizeof(ia) / sizeof(int);  for (int ix = 0; ix != ia_sz; ++ix) {      cout << ia[ix] << endl;   }     cout << endl;      for (auto i : ia) {      cout << i << endl;   }    for (auto beg = begin(ia), last = end(ia); beg != last; ++beg) {      cout << *beg << endl;  }      int ix[3][3] = { { 1,2,3 },{ 4,5,6 },{ 7,8,9 } };int(*pix)[3] = ix;  //pix是一个指针,指向有4个整数的数组int(*pa)[3] = ix;//cout << (*pa+1)[2]  << endl;cout << *pa[0] <<" "<< *pa << endl;cout << *pa[1] << endl;cout << *pa[2] << endl;// int (*p)[3],保存的是3个数组的首地址   // p默认指向第一个数组 { 1,2,3 }  p++ 下一个数组元素 : { 4,5,6 }  p++: { 7,8,9 }for (int (*p)[3] = ix; p!=ix+3; p++ ){//  p是一个数组的地址,解引用(*p)后得到,数组的首地址for (int *px = *p; px!= *p+3; ++px){cout << *px << ' ';}cout << endl;}cout << endl;// int (*p)[3] p是一个指针,指向有3个整数的数组for (auto p = begin(ix); p != end(ix); ++p){for (auto q = begin(*p); q != end(*p); ++q){cout << *q << " ";}cout << endl;} 

原创粉丝点击