- class gcData{
- public:
- double* OneDimArr;
- int OneDimLength;
- double** TwoDimArr;
- int TwoDimX;
- int TwoDimY;
- //构造函数
- gcData(){
- this->OneDimArr = NULL;
- this->OneDimLength = 0;
- this->TwoDimArr = NULL;
- this->TwoDimX = 0;
- this->TwoDimY = 0;;
- }
- gcData(int length){
- this->OneDimArr = (double*)malloc(length*sizeof(double));
- int i;
- for (i = 0; i < length; i++){
- this->OneDimArr[i] = 0;
- }
- this->OneDimLength = length;
- this->TwoDimArr = NULL;
- this->TwoDimX = 0;
- this->TwoDimY = 0;;
- }
- gcData(int x, int y){
- this->OneDimArr = NULL;
- this->OneDimLength = 0;
- int i,j;
- this->TwoDimArr = (double**)malloc(x*sizeof(double));
- for (i = 0; i < x; i++){
- this->TwoDimArr[i] = (double*)malloc(y*sizeof(double));
- for (j = 0; j < y; j++){
- this->TwoDimArr[i][j] = 0;
- }
- }
- this->TwoDimX = x;
- this->TwoDimY = y;
- }
- gcData(int length, int x, int y){
- this->OneDimArr = (double*)malloc(length*sizeof(double));
- this->OneDimLength = length;
- int i;
- this->TwoDimArr = (double**)malloc(x*sizeof(double));
- for (i = 0; i < x; i++){
- this->TwoDimArr[i] = (double*)malloc(y*sizeof(double));
- }
- this->TwoDimX = x;
- this->TwoDimY = y;;
- }
- gcData(double* arr, int length){
- this->OneDimArr = (double*)malloc(length*sizeof(double));
- //赋值
- int i;
- for (i = 0; i < length; i++){
- this->OneDimArr[i] = arr[i];
- }
- this->OneDimLength = length;
- this->TwoDimArr = NULL;
- this->TwoDimX = 0;
- this->TwoDimY = 0;
- }
- gcData(double**arr2,int x, int y){
- this->OneDimArr = NULL;
- this->OneDimLength = 0;
- //申请内存
- int i, j;
- this->TwoDimArr = (double**)malloc(x*sizeof(double));
- for (i = 0; i < x; i++){
- this->TwoDimArr[i] = (double*)malloc(y*sizeof(double));
- for (j = 0; j < y; j++){
- this->TwoDimArr[i][j] = arr2[i][j];
- }
- }
- this->TwoDimX = x;
- this->TwoDimY = y;
- }
- gcData(double* arr,int length,double** arr2, int x, int y){
- //申请内存
- this->OneDimArr = (double*)malloc(length*sizeof(double));
- //赋值
- int i,j;
- for (i = 0; i < length; i++){
- this->OneDimArr[i] = arr[i];
- }
- this->OneDimLength = length;
- //申请内存
- this->TwoDimArr = (double**)malloc(x*sizeof(double));
- for (i = 0; i < x; i++){
- this->TwoDimArr[i] = (double*)malloc(y*sizeof(double));
- for (j = 0; j < y; j++){
- this->TwoDimArr[i][j] = arr2[i][j];
- }
- }
- this->TwoDimX = x;
- this->TwoDimY = y;
- }
- //析构函数
- ~gcData(){
- //释放内存
- if (this->OneDimArr != NULL && this->OneDimLength > 0){
- delete OneDimArr;
- OneDimArr = NULL;
- }
- if (this->TwoDimArr != NULL && (this->TwoDimX > 0 && this->TwoDimY > 0)){
- int i = 0;
- for (i = 0; i<TwoDimX; i++){
- if (this->TwoDimArr[i] != NULL){
- free(TwoDimArr[i]);
- TwoDimArr[i] = NULL;
- }
- }
- free(TwoDimArr);
- TwoDimArr = NULL;
- }
- }
- //获取函数
- void gcWrite(char* filename1, char* filename2){
- if (this->OneDimArr != NULL && this->OneDimLength > 0){
- //写入一维
- ofstream out(filename1);
- int length = this->OneDimLength;
- int i = 0;
- if (out.is_open()){
- for (i = 0; i < length; i++){
- out << this->OneDimArr[i] << "\\n";
- }
- }
- out.close();
- }
- if (this->TwoDimArr != NULL && (this->TwoDimX>0 && this->TwoDimY > 0)){
- //写入二维数组
- ofstream out(filename2);
- int src_x = this->TwoDimX;
- int src_y = this->TwoDimY;
- int i = 0;
- int j = 0;
- if (out.is_open()){
- for (i = 0; i < src_x; i++){
- for (j = 0; j < src_y; j++){
- out << this->TwoDimArr[i][j] << " , ";
- }
- out << "\\n";
- }
- }
- out.close();
- }
- }
- //随机赋值
- void gcRand(int mx){
- int tmpMx = 1;
- int i = 0;
- int j = 0;
- for (i = 0; i < mx; i++){
- tmpMx *= 10;
- }
- int tmpRand1, tmpRand2;
- double tmpRand3;
- srand((unsigned)time(NULL));//设置随机种子
- if (this->OneDimArr != NULL && this->OneDimLength > 0){
- for (i = 0; i<this->OneDimLength; i++){
- this->OneDimArr[i] = (rand() % tmpMx) / double(tmpMx);
- }
- }
- if (this->TwoDimArr != NULL && (this->TwoDimX > 0 && this->TwoDimY > 0)){
- for (i = 0; i < this->TwoDimX; i++){
- for (j = 0; j < this->TwoDimY; j++){
- tmpRand1 = rand();
- this->TwoDimArr[i][j] = (rand() % tmpMx) / double(tmpMx);
- }
- cout << endl;
- }
- }
- }
- void Display(){
- cout << "length:" << this->OneDimLength << endl;
- cout << "X:" << this->TwoDimX << endl;
- cout << "Y:" << this->TwoDimY << endl;
- }
- void gcRandSampleCopyCols(gcData src,int sample){
- if (sample > this->TwoDimY || sample > src.TwoDimY){
- cout << "取的样本数多于总样本数,请检查" << endl;
- return;
- }
- if (src.TwoDimX != this->TwoDimX){
- cout << "矩阵维度不一致!" << endl;
- return;
- }
- //用alloca申请的内存在函数完成之后就会注销,而用malloc申请的需要手动注销
- int *tmp = (int*)alloca(sample*sizeof(int));
- gcFunction::gcRandSampleSeq(src.TwoDimY, sample, tmp, false);
- int i,j;
- for (j = 0; j< sample; j++){
- for (i = 0; i < this->TwoDimX; i++){
- //将随机的列数拷贝到当前对象
- this->TwoDimArr[i][j] = src.TwoDimArr[i][tmp[j]];
- }
- }
- }
- };
- //该片段来自于http://www.codesnippet.cn/detail/2807201513235.html
来源: http://www.codesnippet.cn/detail/2807201513235.html