C++技巧之二维动态数组类模板
作者 佚名技术
来源 程序设计
浏览
发布时间 2012-06-29
面的测试程序: #include "Array.h" #include "Array2D.h" #include <iostream> #include <string> using std::string; using std::cout; using std::endl; void testArray(); void testArray2D(); int main() { testArray(); testArray2D(); return EXIT_SUCCESS; } void testArray() { //普通数组 Array<int> a1(3); cout<<"Testing Array: print 1"<<endl; cout<<a1<<endl; //整体打印 unsigned int i; for(i=0; i<a1.getSize(); ++i) { a1[i]=i+1; //逐个赋值 } cout<<"Testing Array: print 2"<<endl; for(i=0; i<a1.getSize(); ++i) { cout<<a1[i]<<"\t"; //逐个打印 } cout<<endl; //常量数组 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"& |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |