c++ - 2d 向量 c++、OOP 的迭代器设计模式
问题描述
我是 C++ 的初学者。在我的程序中,我创建了一个矩阵并编写了一些函数来操作它的列和行。而不是i
和j
计数器我想实现迭代器设计模式在这里没有找到合适的信息。你能帮我完成这个任务吗?这是我的代码:
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
#define C 5
template <class T>
class myMatrix
{
private:
int col, row;
vector<vector<T>> matrix;
public:
myMatrix(int a, int b)
{
col = b;
row = a;
matrix.assign(a, vector<T>(b));
}
void copy_row(int numb);
void copy_col(int numb);
void input();
void output();
~myMatrix() { }
};
template <class T>
void myMatrix<T>::copy_row(int numb)
{
for (int i = 0; i < col; i++)
cout << matrix[numb][i] << " ";
}
template <class T>
void myMatrix<T>::copy_col(int numb)
{
for (int i = 0; i < row; i++)
cout << matrix[i][numb] << endl;
}
template <class T>
void myMatrix<T>::input()
{
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
{
T number;
cin >> matrix[i][j];
}
}
template <class T>
void myMatrix<T>::output()
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
cout << setw(10) << left << matrix[i][j];
cout << endl;
}
cout << endl;
}
int main()
{
int a, b;
myMatrix<int> matrix1(5, 5);
cout << "Put in the matrix 5x5: " << endl;
matrix1.input();
cout << endl;
matrix1.output();
cout << "Put the column number to copy: ";
cin >> a;
cout << endl;
matrix1.copy_col(a - 1);
cout << endl;
cout << "Put the row number to copy: ";
cin >> b;
cout << endl;
matrix1.copy_row(b - 1);
cout << endl;
return 0;
}
解决方案
推荐阅读
- json - 如何从 Excel 中提取键值对并使用 Python 和/或在线转换器格式转换为 JSON
- docker-compose - 如何通过 docker 运行 gluu?我需要一个 docker-compose 示例
- windows - 查找多个文件并将它们复制到PowerShell中的另一个位置
- php - 根据当前日期计算日期范围
- java - W / System.err:java.io.IOException:无法运行程序“python”:错误= 13,权限被拒绝
- amazon-web-services - Elastic Beanstalk Worker 层构建失败并出现 SQSD 错误
- bash - How to `command file.txt OR another_file.txt` in CLI (bash/zsh)
- ruby-on-rails - Ruby on rails 错误
- json - 如何在 JSON Schema Draft-4 中创建所需的对象键?
- ubuntu-18.04 - vpnc-scripts/vpnc-script: 找不到带有 openconnect 的 ubuntu 18.04