实验任务五
vectorInt.hpp
1 #include <iostream> 2 #include <cassert> 3 using namespace std; 4 5 class vectorInt { 6 7 public: 8 vectorInt(int n); 9 vectorInt(int n, int value); 10 vectorInt(const vectorInt &x); 11 ~vectorInt(); 12 13 int &at(int i); //注意,要返回引用类型 14 int get_size() { return size; } 15 16 friend void output(vectorInt &x); //形参要为引用类型,否则还要调用复制构造函数 17 18 private: 19 int* arr; 20 int size; 21 }; 22 23 vectorInt::vectorInt(int n): size{n} { 24 arr = new int[size]; 25 cout << "constructor1 called, dynamic arr created..." << endl; 26 } 27 28 vectorInt::vectorInt(int n, int value): size{n} { 29 arr = new int[size](); 30 for(int i = 0; i < size; i++) { 31 arr[i] = value; 32 } 33 cout << "constructor2 called, dynamic arr created..." << endl; 34 } 35 //复制构造函数(深复制) 36 vectorInt::vectorInt(const vectorInt &x): size{x.size} { 37 arr = new int[size]; 38 for(int i = 0; i < size; i++) { 39 arr[i] = x.arr[i]; 40 } 41 cout << "copy constructor called, array copyed..." << endl; 42 } 43 //析构函数 44 vectorInt::~vectorInt() { 45 cout << "destructor called..." << endl; 46 delete[] arr; 47 } 48 49 int &vectorInt::at(int i) { 50 assert(i >= 0 && i < size); //宏,若不满足条件,终止程序 51 return arr[i]; 52 } 53 // 友元函数, 输出数组元素 54 void output(vectorInt &x) { 55 for(int i = 0; i < x.size; i++) { 56 cout << x.arr[i]; 57 cout << (i < x.size - 1 ? ", " : "\n"); 58 } 59 }
task5.cpp
1 #include <iostream> 2 #include "vectorInt.hpp" 3 4 void test() { 5 using namespace std; 6 7 int n; 8 cin >> n; 9 10 vectorInt x1(n); 11 for(auto i = 0; i < n; ++i) 12 x1.at(i) = i*i; 13 14 output(x1); 15 16 vectorInt x2(n, 42); 17 vectorInt x3(x2); 18 19 output(x2); 20 output(x3); 21 22 x2.at(0) = 77; 23 24 output(x2); 25 output(x3); 26 } 27 28 int main() { 29 test(); 30 }
运行截图
实验任务六
Matrix.hpp
1 #pragma once 2 3 #include <iostream> 4 #include <cassert> 5 6 using std::cout; 7 using std::endl; 8 9 class Matrix { 10 public: 11 Matrix(int n); // 构造函数,构造一个n*n的矩阵 12 Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵 13 Matrix(const Matrix &X); // 复制构造函数,使用已有的矩阵X构造 14 ~Matrix(); //析构函数 15 16 void set(const double *pvalue); // 用pvalue指向的连续内存块数据按行为矩阵赋值 17 void set(int i, int j, int value); //设置矩阵第i行第j列元素值为value 18 double &at(int i, int j); //返回矩阵第i行第j列元素的引用 19 double at(int i, int j) const; // 返回矩阵第i行第j列元素的值 20 int get_lines() const; //返回矩阵行数 21 int get_cols() const; //返回矩列数 22 void print() const; // 按行打印输出矩阵 23 24 private: 25 int lines; // 矩阵行数 26 int cols; // 矩阵列数 27 double *p; // 指向存放矩阵数据的内存块的首地址 28 }; 29 30 // 类Matrix的实现:待补足 31 32 Matrix::Matrix(int n): lines{n}, cols{n} { 33 p = new double[n * n](); //数组元素有默认值 34 } 35 36 Matrix::Matrix(int n, int m): lines{n}, cols{m} { 37 p = new double[n * m](); 38 } 39 40 Matrix::Matrix(const Matrix &X): lines{X.lines}, cols{X.cols} { 41 p = new double[lines * cols](); 42 for(int i = 0; i < lines * cols; i++) { 43 p[i] = X.p[i]; 44 } 45 } 46 47 Matrix::~Matrix() { 48 delete[] p; 49 } 50 //此处假设该一维数组长度 == 矩阵lines*cols 51 void Matrix::set(const double *pvalue) { 52 int n = cols * lines; 53 for(int i = 0; i < n; i++) { 54 p[i] = pvalue[i]; 55 } 56 } 57 58 void Matrix::set(int i, int j, int value) { 59 p[i * cols + j] = value; 60 } 61 62 double &Matrix::at(int i, int j) { 63 assert(i >= 0 && i < lines && j >= 0 && j < cols); 64 return p[i * cols + j]; 65 } 66 67 double Matrix::at(int i, int j) const { 68 assert(i >= 0 && i < lines && j >= 0 && j < cols); 69 return p[i * cols + j]; 70 } 71 72 int Matrix::get_lines() const { 73 return lines; 74 } 75 76 int Matrix::get_cols() const { 77 return cols; 78 } 79 80 void Matrix::print() const { 81 for(int i = 0; i < lines; i++) { 82 for(int j = 0; j < cols; j++) { 83 cout << p[i * cols + j]; 84 cout << ((j == cols - 1) ? "\n" : ", "); 85 } 86 } 87 }
task6.cpp
#include <iostream> #include <cassert> #include "matrix.hpp" void test() { using namespace std; double x[] = {3, 4, 5, 6, 7, 8}; Matrix m1(3, 2); // 创建一个3×2的矩阵 m1.set(x); // 用一维数组x的值按行为矩阵m1赋值 m1.print(); // 打印矩阵m1的值 cout << "the first line is: " << endl; cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl; // 输出矩阵m1第1行两个元素的值 cout << endl; Matrix m2(2, 3); m2.set(x); m2.print(); cout << "the first line is: " << endl; cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl; cout << endl; Matrix m3(m2); // 用矩阵m2构造新的矩阵m3 m3.set(0, 0, 999); // 将矩阵m3第0行第0列元素值设为999 m3.print(); } int main() { test(); }
运行截图
总结
对于二维数组a[row][col]来说,它的每一个元素的数据类型实际上是一种一维数组,也就是int[col]。
要在堆区创建一个二维数组,二维数组的第一维必须是一个常量(因为必须确定数组元素int【col】的类型)
标签:const,Matrix,int,lines,cols,实验,数组,vectorInt,指针 From: https://www.cnblogs.com/youxiasaigao/p/16859926.html