#ifndef VECTOR_H_INCLUDE
#define VECTOR_H_INCLUDE
#include <vector>
using namespace std;
template <class T>
class Vector2D
{
public:
Vector2D():row(0), col(0){;}
Vector2D(int x, int y)
{
row = x;
col = y;
for (int i = 0; i < x; i++)
{
vector<T> xvec(col);
vec2D.push_back(xvec);
}
}
Vector2D(int x, int y, T* arr)
{
row = x;
col = y;
for (int i = 0; i < x; i++)
{
vector<T> xvec(col);
vec2D.push_back(xvec);
}
T* p = arr;
for(size_t i = 0; i < row; i++)
{
for(size_t j = 0; j < col; j++)
{
vec2D[i][j] = *p++;
}
}
}
Vector2D(int x, int y, vector<T> vec)
{
row = x;
col = y;
for (int i = 0; i < x; i++)
{
vector<T> xvec(col);
vec2D.push_back(xvec);
}
int k = 0;
for(size_t i = 0; i < row; i++)
{
for(size_t j = 0; j < col; j++)
{
vec2D[i][j] = vec[k++];
}
}
}
void Rerow(size_t newSize)
{
if(newSize == row)
{
return;
}
if (newSize < row)
{
for(size_t i = 0 ; i < row-newSize; i++)
{
vec2D.pop_back();
}
}
else
{
for(size_t i = 0 ; i < newSize-row; i++)
{
vector<int> xvec(col);
vec2D.push_back(xvec);
}
}
row = newSize;
}
void Recol(size_t newSize)
{
col = newSize;
for (size_t i = 0; i < row; i++)
{
vec2D[i].Resize(newSize);
}
}
void Resize(size_t newRow, size_t newCol)
{
Rerow(newRow);
Recol(newCol);
}
size_t GetSize()
{
if(row == col)
{
return row;
}
else
{
return 0;
}
}
vector<T>& operator[](size_t x)
{
if(x >= row)
{
throw std::out_of_range("Out of range");
}
return vec2D[x];
}
friend ostream& operator<< (ostream& os, const Vector2D<T>& vec)
{
for(size_t i = 0; i < vec.row; i++)
{
for(size_t j = 0; j < vec.col; j++)
{
os << vec.vec2D[i][j] << " ";
}
os << "/n";
}
return os;
}
private:
vector< vector <T> > vec2D;
size_t row;
size_t col;
};
#endif