首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 计算机考试 > 等级考试 > 复习指导 >

C++技巧——二维动态数组类模板(3)

2009-04-28 
二维动态数组类模板

    //常量数组
  const Array<int> a2(3, 123, 234, 345);
  cout<<"Testing Array: print 3"<<endl;
  cout<<a2<<endl;
  cout<<"Testing Array: print 4"<<endl;
  for(i=0; i<a2.getSize(); ++i) {
  cout<<a2[i]<<"\t";
  }
  cout<<endl;
  //拷贝构造
  Array<int> a3(a1);
  cout<<"Testing Array: print 5"<<endl;
  cout<<a3<<endl;
  const Array<int> a4(a2);
  cout<<"Testing Array: print 6"<<endl;
  cout<<a4<<endl;
  //数组赋值给数组
  Array<int> a5;
  a5=a4;
  cout<<"Testing Array: print 7"<<endl;
  cout<<a5<<endl;
  //增减数组元素
  a5.setSize(a5.getSize()+1);
  a5[a5.getSize()-1]=111;
  cout<<"Testing Array: print 8"<<endl;
  cout<<a5<<endl;
  a5.setSize(a5.getSize()-2);
  cout<<"Testing Array: print 9"<<endl;
  cout<<a5<<endl;
  //保存对象
  Array<string> a6(2, string("str0"), string("str1"));
  cout<<"Testing Array: print 10"<<endl;
  cout<<a6<<endl;
  for(i=0; i<a6.getSize(); ++i) {
  a6[i]+=static_cast<char>(48+i);
  }
  cout<<"Testing Array: print 11"<<endl;
  for(i=0; i<a6.getSize(); ++i) {
  cout<<a6[i]<<"\t";
  }
  cout<<endl;
  const Array<string> a7(3, string("000"), string ("111"), string("222"));
  cout<<"Testing Array: print 12"<<endl;
  cout<<a7<<endl;
  for(i=0; i<a7.getSize(); ++i) {
  cout<<a7[i]<<"\t";
  }
  cout<<endl;
  }
  void testArray2D() {
  //普通数组
  Array2D<int> a1(2, 2);
  cout<<"Testing Array2D: print 1"<<endl;
  cout<<a1<<endl;
  unsigned int i,j;
  for(i=0; i<a1.getRows(); ++i) {
  for(j=0; j<a1.getCols(); ++j) {
  a1[i][j]=i+j;
  }
  }
  cout<<"Testing Array2D: print 2"<<endl;
  for(i=0; i<a1.getRows(); ++i) {
  for(j=0; j<a1.getCols(); ++j) {
  cout<<a1[i][j]<<"\t";
  }
  cout<<endl;
  }
  //常量数组
  const Array2D<int> a2(2, 2, 123, 234, 345, 456);
  cout<<"Testing Array2D: print 3"<<endl;
  cout<<a2<<endl;
  cout<<"Testing Array2D: print 4"<<endl;
  for(i=0; i<a2.getRows(); ++i) {
  for(j=0; j<a2.getCols(); ++j) {
  cout<<a2[i][j]<<"\t";
  }
  cout<<endl;
  }
  //拷贝构造
  Array2D<int> a3(a1);
  cout<<"Testing Array2D: print 5"<<endl;
  cout<<a3<<endl;
  const Array2D<int> a4(a2);
  cout<<"Testing Array2D: print 6"<<endl;
  cout<<a4<<endl;
  //数组赋值给数组
  Array2D<int> a5;
  a5=a4;
  cout<<"Testing Array2D: print 7"<<endl;
  cout<<a5<<endl;
  //保存对象
  Array2D<string> a6(2, 2, string("str00"), string("str01"), string("str10"), string("str11"));
  cout<<"Testing Array2D: print 8"<<endl;
  cout<<a6<<endl;
  for(i=0; i<a6.getRows(); ++i) {
  for(j=0; j<a6.getCols(); ++j) {
  a6[i][j]+=static_cast<char>(48+i+j);
  }
  }
  cout<<"Testing Array2D: print 9"<<endl;
  for(i=0; i<a6.getRows(); ++i) {
  for(j=0; j<a6.getCols(); ++j) {
  cout<<a6[i][j]<<"\t";
  }
  cout<<endl;
  }
  const Array2D<string> a7(2, 1, string("11111"), string("22222"));
  cout<<"Testing Array2D: print 10"<<endl;
  cout<<a7<<endl;
  cout<<"Testing Array2D: print 11"<<endl;
  for(i=0; i<a7.getRows(); ++i) {
  for(j=0; j<a7.getCols(); ++j) {
  cout<<a7[i][j]<<"\t";
  }
  cout<<endl;
  }
  }

 

3COME考试频道为您精心整理,希望对您有所帮助,更多信息在http://www.reader8.com/exam/

热点排行