成都市市民服务热线:C 技巧:二维动态数组类模板
来源:百度文库 编辑:偶看新闻 时间:2024/06/11 13:57:51
第一个头文件Array.h是一维动态数组类模板:
//Array.h
#ifndef CARL_SEN_ARRAY_H
#define CARL_SEN_ARRAY_H
#include
#include
using std::out_of_range;
using std::ostream;
template
class Array {
protected:
unsigned int size;
T* data;
public:
//构造函数
Array(unsigned int _size=0);
Array(unsigned int count, T data1, ...);
//复制控制
Array(const Array
Array& operator=(const Array
~Array() {
delete[] data;
}
//两种重载运算符
T& operator[](unsigned int index);
const T& operator[](unsigned int index) const;
friend ostream& operator<<(ostream& os, const Array
//get,set成员函数
unsigned int getSize() const {
return size;
}
void setSize(unsigned int newSize);
};
template
Array
for(unsigned int i=0; i
}
}
template
Array
va_list ap;
va_start(ap, count);
for(unsigned int i=0; i
}
va_end(ap);
}
template
Array
for(unsigned int i=0; i
}
}
template
Array
if(&array!=this) {
delete[] data;
data=new T[array.size];
size=array.size;
for(unsigned int i=0; i
}
}
return *this;
}
template
T& Array
if(index>=size) {
throw out_of_range("invalid index");
}
return data[index];
}
template
const T& Array
if(index>=size) {
throw out_of_range("invalid index");
}
return data[index];
}
template
void Array
T* const newData=new T[newSize];
const unsigned int Min=size
}
delete[] data;
data=newData;
size=newSize;
}
template
ostream& operator<<(ostream& os, const Array
for(int i=0; i
return os;
}
#endif
第二个头文件Array2D.h是二维动态数组类模板:
//Array2D.h
#ifndef CARL_SEN_ARRAY2D_H
#define CARL_SEN_ARRAY2D_H
#include "Array.h"
#include
#include
using std::ostream;
using std::out_of_range;
template
class Array2D {
protected:
unsigned int rows;
unsigned int cols;
Array
public:
//构造函数
Array2D(unsigned int _rows=0, unsigned int _cols=0);
Array2D(unsigned int _rows, unsigned int _cols, T data1, ...);
//两种重载运算符
class RowArray;
RowArray operator[](unsigned int row);
const RowArray operator[](unsigned int row) const;
friend ostream& operator<<(ostream& os, const Array2D
//计算第二维下标操作的嵌套类
class RowArray {
private:
const Array2D
unsigned int row;
public:
RowArray(Array2D
array2D(_array2D), row(_row) {}
RowArray(const Array2D
array2D(_array2D), row(_row) {}
T& operator[](unsigned int col) {
if(col>=array2D.cols) {
throw out_of_range("invalid col");
}
return const_cast
}
const T& operator[](unsigned int col) const {
if(col>=array2D.cols) {
throw out_of_range("invalid col");
}
return array2D.array[row*array2D.cols+col];
}
};
friend class RowArray;
//get,set函数
unsigned int getRows() const {
return rows;
}
unsigned int getCols() const {
return cols;
}
};
template
Array2D
rows(_rows), cols(_cols), array(_rows*_cols){
for(unsigned int i=0; i
}
}
template
Array2D
rows(_rows), cols(_cols),array(_rows*_cols){
va_list ap;
va_start(ap, _cols);
for(unsigned int i=0; i
}
va_end(ap);
}
template
Array2D
if(row>=rows) {
throw out_of_range("invalid row");
}
return RowArray(*this, row);
}
template
const Array2D
if(row>=rows) {
throw out_of_range("invalid row");
}
return RowArray(*this, row);
}
template
ostream& operator<<(ostream& os, const Array2D
unsigned int i, j;
for(i=0; i
if(i!=array2D.rows-1) {
os<
}
return os;
}
#endif
现在就可以使用它们了,如下面的测试程序:
#include "Array.h"
#include "Array2D.h"
#include
#include
using std::string;
using std::cout;
using std::endl;
void testArray();
void testArray2D();
int main() {
testArray();
testArray2D();
return EXIT_SUCCESS;
}
void testArray() {
//普通数组
Array
cout<<"Testing Array: print 1"<
unsigned int i;
for(i=0; i
}
cout<<"Testing Array: print 2"<
cout<
//常量数组
const Array
cout<<"Testing Array: print 3"<
cout<
//拷贝构造
Array
cout<<"Testing Array: print 5"<
cout<<"Testing Array: print 6"<
//数组赋值给数组
Array
a5=a4;
cout<<"Testing Array: print 7"<
//增减数组元素
a5.setSize(a5.getSize()+1);
a5[a5.getSize()-1]=111;
cout<<"Testing Array: print 8"<
cout<<"Testing Array: print 9"<
//保存对象
Array
cout<<"Testing Array: print 10"<
}
cout<<"Testing Array: print 11"<
cout<
cout<<"Testing Array: print 12"<
cout<
void testArray2D() {
//普通数组
Array2D
cout<<"Testing Array2D: print 1"<
for(i=0; i
}
}
cout<<"Testing Array2D: print 2"<
cout<
//常量数组 //数组赋值给数组 //保存对象
const Array2D
cout<<"Testing Array2D: print 3"<
cout<
//拷贝构造
Array2D
cout<<"Testing Array2D: print 5"<
cout<<"Testing Array2D: print 6"<
Array2D
a5=a4;
cout<<"Testing Array2D: print 7"<
Array2D
cout<<"Testing Array2D: print 8"<
}
}
cout<<"Testing Array2D: print 9"<
cout<
const Array2D
cout<<"Testing Array2D: print 10"<
cout<
}