C++实现具有[数组]相似特征的类DoubleSubscriptArray
生活随笔
收集整理的這篇文章主要介紹了
C++实现具有[数组]相似特征的类DoubleSubscriptArray
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <iostream>
using namespace std;class DoubleSubscriptArray {public:DoubleSubscriptArray(int x, int y) {p = new int *[x];//行 //申請行的空間for (int i = 0; i < x; i++) {p[i] = new int [y];//每行的列申請空間}for (int i = 0; i < x; i++)for (int j = 0; j < y; j++)p[i][j] = 0;nx = x;ny = y;}int &operator()(int x, int y) {return p[x][y];}const int &operator()(int x, int y) const {return p[x][y];}bool operator==(DoubleSubscriptArray &a) {if (a.nx != nx || a.ny != ny)return false;else {for (int i = 0; i < a.nx; i++)for (int j = 0; j < a.ny; j++) {if (a.p[i][j] != p[i][j])return false;}return true;}}bool operator!=(DoubleSubscriptArray &a) {return !(a == *this);}friend ostream &operator<<(ostream &os, const DoubleSubscriptArray &a);friend istream &operator>>(istream &is, DoubleSubscriptArray &a);public:~DoubleSubscriptArray() {for (int i = 0; i < nx; i++)delete[] p[i];delete[] p;}public:void print(int x, int y) {for (int i = 0; i < x; i++)for (int j = 0; j < y; j++) {cout << p[i][j] << endl;}}private:int **p;int nx, ny;};ostream &operator<<(ostream &os, const DoubleSubscriptArray &a) {for (int i = 0; i < a.nx; i++) {for (int j = 0; j < a.ny; j++)os << a.p[i][j] << " ";os << endl;}return os;
}istream &operator>>(istream &is, DoubleSubscriptArray &a) {for (int i = 0; i < a.nx; i++) {for (int j = 0; j < a.ny; j++)is >> a.p[i][j];}return is;
}int main() {DoubleSubscriptArray a(3, 4);DoubleSubscriptArray c(3, 4);DoubleSubscriptArray d(3, 4);cin >> d;cout << d << endl;a(0, 2) = 100;if (a != c)cout << "yes" << endl;int b = a(0, 2);cout << b << endl;a.print(3, 4);cout << a << endl;return 0;
}
總結
以上是生活随笔為你收集整理的C++实现具有[数组]相似特征的类DoubleSubscriptArray的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 瘦屁股和大腿根部最快最有效的方法有什么
- 下一篇: 脚踩减肥振动机真的有用吗