首页 > 其他分享 >实验4 类与数组、指针

实验4 类与数组、指针

时间:2022-11-08 19:57:50浏览次数:45  
标签:Matrix int 矩阵 lines cols 实验 数组 vectorInt 指针

  • Task1~4

浅复制:int a = 10; int b = a; 可用于静态内存的复制。对于简单的类,默认的复制构造函数已经够用了,但当类持有其他资源,如动态分配的内存、指针等,就需要用到深复制。

深复制:分配动态内存,再将原有对象的内存复制过来。原有对象和新对象所持有的动态内存是相互独立的。

  • Task 5

vectorInt.hpp

 1 #pragma once
 2 #include<iostream>
 3 #include<cassert>
 4 
 5 using std::cout;
 6 using std::cin;
 7 
 8 class vectorInt {
 9 public:
10     vectorInt(int n); //构造函数
11     vectorInt(int n,int value); //构造函数
12     vectorInt(const vectorInt& vi); //复制构造函数
13     ~vectorInt(); //析构函数
14     int& at(int index);
15     friend void output(vectorInt& vi);
16 private:
17     int size;
18     int* p;
19 };
20 
21 vectorInt::vectorInt(int n) {
22     p = new int[n];
23     size = n;
24     cout << "constructor 1 called.\n";
25 }
26 
27 vectorInt::vectorInt(int n, int value) {
28     p = new int[n]();
29     size = n;
30     for (int i = 0; i < size; ++i) {
31         p[i] = value;
32     }
33     cout << "constructor 2 called. \n";
34 }
35 
36 vectorInt::vectorInt(const vectorInt& vi) {
37     size = vi.size;
38     //p = vi.p; //直接将vi的元素p的内存复制过来,应该是浅复制,运行main函数调用析构时出错
39     p = new int[vi.size];
40     for (int i = 0; i < size; ++i) {
41         p[i] = vi.p[i];
42     }//给p动态分配了内存空间,再复制内存,应该是深复制,运行正确
43     cout << "copy constructor called.\n";
44 }
45 
46 vectorInt::~vectorInt(){
47     delete[] p; //要加上[]
48     cout << "destructor called. \n";
49 }
50 
51 int& vectorInt::at(int index) {//返回下标为index的元素引用
52     assert(index >= 0 && index < size);
53     return p[index];
54 }
55 
56 void output(vectorInt& vi) {//输出
57     for (int i = 0; i < vi.size; ++i) {
58         cout << vi.p[i] << ", ";
59     }
60     cout << "\b\b \n";
61 }

task5.hpp

 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 }

测试结果:

 

 

 

 

  • Task 6

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);          // 复制构造函数
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 // 构造函数,n*n矩阵
32 Matrix::Matrix(int n) {
33     p = new double[n * n];
34     lines = n;
35     cols = n;
36 }
37 // 构造函数,n*m矩阵
38 Matrix::Matrix(int n, int m){
39     p = new double[n * m];
40     lines = n;
41     cols = m;
42 }
43 // 复制构造函数
44 Matrix::Matrix(const Matrix& X){
45     p = new double[X.lines * X.cols];
46     lines = X.lines;
47     cols = X.cols;
48     for (auto i = 0; i < X.lines * X.cols; ++i) {
49         p[i] = X.p[i];
50     }
51 }
52 //析构函数
53 Matrix::~Matrix(){
54     delete[]p;
55 }
56 
57 // 用pvalue指向内存块数据按行为矩阵赋值
58 void Matrix::set(const double* pvalue){
59     for (auto i = 0; i < sizeof(pvalue) && i < lines * cols; i++) {//退出循环的条件为i没到pvalue的长度或i小于矩阵的大小
60         p[i] = pvalue[i];
61     }
62 }
63 //设置矩阵第i行第j列元素值为value
64 void Matrix::set(int i, int j, int value){
65     p[i * lines + j] = value;
66 }
67 //返回矩阵第i行第j列元素的引用
68 double& Matrix::at(int i, int j){
69     assert(i >= 0 && i < lines);
70     assert(j >= 0 && j < cols);
71     return p[i * lines + j];
72 }
73 // 返回矩阵第i行第j列元素的值
74 double Matrix::at(int i, int j) const{
75     return p[i * lines + j];
76 }
77 //返回矩阵行数
78 int Matrix::get_lines() const{
79     return lines;
80 }
81 //返回矩阵列数
82 int Matrix::get_cols() const{
83     return cols;
84 }
85 //按行打印矩阵
86 void Matrix::print() const{
87     for (auto i = 0,j = 0; i < lines * cols; i++) {
88         cout << p[i] << ", ";
89         j++;
90         if (j == cols){//若打完一行就换行
91             cout << "\b\b \n";
92             j = 0;
93         }
94     }
95 }

task6.cpp

 1 #include <iostream>
 2 #include "matrix.hpp"
 3 
 4 void test() {
 5     using namespace std;
 6 
 7     double x[] = { 1, 2, 3, 4, 5, 6 };
 8 
 9     Matrix m1(3, 2);
10     m1.set(x);
11     m1.print();
12     cout << "the first line is: " << endl;
13     cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl;
14     cout << endl;
15 
16     Matrix m2(2, 3);
17     m2.set(x);
18     m2.print();
19     cout << "the first line is: " << endl;
20     cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl;
21     cout << endl;
22 
23     Matrix m3(m2);
24     m3.set(0, 0, 999);
25     m3.print();
26 }
27 
28 int main() {
29     test();
30 }

测试结果1:

 

 测试结果2:

  更换后的测试数据——

 

   测试结果

 

 

别的收获:

  在编写task6代码时测试一下代码,发现 cout 的获取输出的数据的顺序是从后往前的。

 

标签:Matrix,int,矩阵,lines,cols,实验,数组,vectorInt,指针
From: https://www.cnblogs.com/lwhhhh/p/16870920.html

相关文章

  • 软件工程最大连续子数组和(最大子段和)
     题目一:最大连续子数组和(最大子段和)背景 问题:给定n个整数(可能为负数)组成的序列a[1],a[2],a[3],…,a[n],求该序列如a[i]+a[i+1]+…+a[j]的子段和的最大值。当所给的整......
  • 实验四
    实验任务五cpp#include<iostream>#include"vectorInt.hpp"voidtest(){usingnamespacestd;intn;cin>>n;vectorIntx1(n);for(aut......
  • 第一次实验题目
    1.题目:最大连续子数组和(最大子段和)问题:给定n个整数(可能为负数)组成的序列a[1],a[2],a[3],…,a[n],求该序列如a[i]+a[i+1]+…+a[j]的子段和的最大值。当所给的整数均为负数......
  • 实验四
    //task.5.cpp#include<iostream>#include"vectorInt.hpp"voidtest(){usingnamespacestd;intn;cin>>n;vectorIntx1(n);for(au......
  • 实验四类和对象数组及指针
    11#pragmaonce22#include<iostream>3344usingstd::cout;55usingstd::endl;6677classvectorInt{88public:99//构造......
  • mysql根据json字段内容作为查询条件(包括json数组)检索数据
    最近用到了mysql5.7的json字段的检索查询,发现挺好用的,记录一下笔记我们有一个日志表,里面的data字段是保存不同对象的json数据,遇到想根据里面的json的字段内容作为条件查询......
  • 8种现代数组方法,每个开发人员都应该知道
    英文| https://javascript.plainenglish.io/8-modern-array-methods-that-every-developer-should-know-416855e01757翻译|小爱在用代码执行数组操作时,你是否经想过,关于......
  • 223201062522-软件工程基础Y- 实验一 刘晋
      沈阳航空航天大学软件工程基础实验报告实验名称:实验一实验题目:个人项目完成时间:2022年11月1实验内容及要求1.1教学内容及要求建立个人博客,完......
  • javascript中数组的22种方法
    数组总共有22种方法,本文将其分为对象继承方法、数组转换方法、栈和队列方法、数组排序方法、数组拼接方法、创建子数组方法、数组删改方法、数组位置方法、数组归并方法和数......
  • 湍流数组 双指针+数学 奇偶分数组 字符串
    978.最长湍流子数组dp[0][0]=dp[0][1]=1;初始化一个数for(inti=1;i<n;i++){dp[i][0]=dp[i][1]=1;if(arr[i]>arr[i-1]){dp[i][0]=dp[i-1][1......