首页 > 其他分享 >类与数组,指针

类与数组,指针

时间:2022-11-04 09:11:31浏览次数:42  
标签:Matrix int cols 数组 x2 vectorInt include 指针

#pragma once

#include <iostream>
#include <algorithm>

using namespace std;

class vectorInt {
public:
    vectorInt(int len)
        : length{len} {
            cout << "constructor 1 called" << endl;
            p = new int[length];
        }
    vectorInt(int len,int val)
        : length{len} {
            cout << "constructor 2 called" << endl;
            p = new int[length];
            for(int i=0;i<length;i++) {
                *(p+i) = val;
            }
        }
    vectorInt(const vectorInt& v)
        : length{v.length} {
            cout << "copy constructor called" << endl;
            p = new int[length];
            for(int i=0;i<length;i++) {
                *(p+i) = v.at(i);
            }
        }
    ~vectorInt() {
        cout << "destructor called" << endl;
        delete[] p;
    }
    int &at(int i) const{
        return *(p+i);
    }
    int get_size() const {
        return length;
    }
    friend void output(const vectorInt& v) {
        for(int i=0;i<v.length;i++) {
            cout << *(v.p+i) << ", ";
        }
        cout << endl;
    }
private:
    int *p;
    int length;
};

  

#include <iostream>
#include "vectorInt.hpp"

void test() {
    using namespace std;

    int n;
    cin >> n;
    
    vectorInt x1(n);
    for(auto i = 0; i < n; ++i)
        x1.at(i) = i*i;

    output(x1);

    vectorInt x2(n, 42);
    vectorInt x3(x2);

    output(x2);
    output(x3);

    x2.at(0) = 77;

    output(x2);
    output(x3);
}

int main() {
    test();
}

  

#pragma once

#include <iostream>

using std::cout;
using std::endl;

class Matrix {
public:
    Matrix(int n)
        : lines{n},cols{n} {
            p = new double[n*n];
        }                  
    Matrix(int n, int m)
        : lines{n},cols{m} {
            p = new double[n*m];
        }      
    Matrix(const Matrix &X)
        : lines{X.lines},cols{X.cols} {
            p = new double[lines*cols];
            for(int i=0;i<lines*cols;i++) {
                p[i] = X.p[i];
            }
        }           
    ~Matrix() {
        delete[] p;
    }                     

    void set(const double *pvalue) {
        for(int i=0;i<lines*cols;i++) {
            p[i] = pvalue[i];
        }
    } 
    void set(int i, int j, int value) {
        p[i*cols+j] = value;
    }
    double &at(int i, int j){
        return p[i*cols+j];
    }        
    double at(int i, int j) const {
        return p[i*cols+j];
    }
    int get_lines() const {
        return lines;
    }           
    int get_cols() const {
        return cols;
    }              
    void print() const {
        for(int i=0;i<lines;i++) {
            for(int j=0;j<cols;j++) {
                cout << p[i*cols+j] << ", ";
            }
            cout << endl;
        }
    }                

private:
    int lines; 
    int cols;  
    double *p;
};

  

#include <iostream>
#include "matrix.hpp"

void test() {
    using namespace std;

    double x[] = {1, 2, 3, 4, 5, 6};

    Matrix m1(3, 2);    
    m1.set(x);        
    m1.print();      
    cout << "the first line is: " << endl;
    cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl;  
    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);    
    m3.set(0, 0, 111);  
    m3.print();
}

int main() {
    test();
}

  

实验总结:

声明指针变量时,必须指定指针所指向变量的类型,因为不同的变量类型占用不同的存储空间。一些指针操作需要知道操作对象的大小。另外,程序必须知道储存在指定地址上的数据类型。实参和形参指向同一个地址,他们只是指向相同,指针变量自身的地址却不相同.也就是说当形参新指向了一块新内存时它不能为实参也同样带回一块内存地址。

标签:Matrix,int,cols,数组,x2,vectorInt,include,指针
From: https://www.cnblogs.com/cflxl/p/16856569.html

相关文章

  • 多维数组扁平化处理的几种方法
    方法一、字符串化+数组化functionflatten(arr){letcount=0;returnarr.toString().split(',').map(function(item){ returnNumber(item)})}fl......
  • 创建数组,增删改查
    创建数组letarr=[];letarr=newArray(1,2,5);letarr=Array.from([1,2,3],item=>item+2);//[3,4,5]letarr=arr1.concat(arr2,arr3);//返回新数组,concat......
  • 实验4 类与数组、指针
    1#pragmaonce2#include<iostream>3usingnamespacestd;4classvectorInt{5public:6vectorInt(intn):size(n){7cout<<"constr......
  • 88.合并两个有序数组
    给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目。请你 合并 nums2 到 nums1 中,使合......
  • 参数传递误用 指针相关
    https://mp.weixin.qq.com/s/VS7DZg9qb_QmqAKqm_zc-Q本文收集一些使用Go开发过程中非常容易踩坑的case,所有的case都有具体的代码示例,以及针对的代码修复方法,以避免大家再......
  • Java学习笔记day3--二维数组
    packageday4_array;importjavax.swing.plaf.synth.SynthFormattedTextFieldUI;//一维数组的元素仍然是一维数组,则构成了二维数组publicclassArrayDemension2{......
  • 福建WC2014 路径权值(Kruskal重构树 + 树状数组)
    题目描述:给定一个带权树,树上任意两点间的路径权值\(d\left(x,y\right)\)定义为\(x,y\)这两个点之间路径上的最小值,树上任意一点x的权值定义为这个点到树上其他所有点......
  • 数组
    数组名称 代表一个数组的第一个元素 intarr[5]; arr={0,1,2,3,4};//错误的写法这里的arr指的是数组的第一个元素即arr[0]不能给一个元素赋值5个值......
  • python描述 LeetCode 1486. 数组异或操作
    python描述LeetCode1486.数组异或操作  大家好,我是亓官劼(qíguānjié),在【亓官劼】公众号、GitHub、B站、华为开发者论坛等平台分享一些技术博文,主要包括前端开发、......
  • C++——指针
    指针基本概念C++的指针也是标识符,不能与其它的普通变量重名;对指针的赋值操作通俗的被称为“指向某变量”,被指向的变量的数据类型称为“基类型”。指针占用的内存指针也......