64,281
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
void input_vector (vector<vector<int> >&);
int main ()
{
vector<vector<int> > vec;
input_vector (vec);
vector<vector<int> >::iterator it = vec.begin ();
for (; it != vec.end (); ++it) {
vector<int>::iterator i = it->begin ();
while (i != it->end ()) {
cout << *i++ << " ";
}
cout << endl;
}
return 0;
}
void input_vector (vector<vector<int> >& vec)
{
vector<int> v;
while (!cin.eof()) {
int tmp(0);
v.clear ();
string line;
getline(cin, line);
istringstream ss(line);
while (ss>>tmp) {
v.push_back (tmp);
}
vec.push_back (v);
}
}
class CMatrix
{
public:
int height;
int width;
vector<int> data;
CMatrix(){height = width = 0;}//默认构造函数
CMatrix(int h, int w, int num=0){height = h; width = w; data.assgin(h*w, 0);} //分配空间的构造函数,默认值全为 num
bool reShape(int h, int w) //重新定义高度、宽度。
{
if(h*w != height*width)
return false;
height = h; width = w;
return true;
}
int* operator[](int h) //重载[],使之支持 CMatrix m; m[0][2] = m[1][2] = 5;之类的用法
{
if( h>= height || data.empty())
return NULL;
else
return (&data[0]) + h * width;
}
};