//2-D vector can be used in this way
#include<iostream>
#include<cstdlib>
#include<vector>
#include<ctime>
using namespace std;
class aj
{
vector<int> v1;
vector<vector<int> > v2;
int m,n;
public:
void input();
void show();
};
void aj::input()
{
int i,j;
cout<<"\nEnter value of rows and column\n";
cin>>m>>n;
srand(time(NULL));
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
v1.push_back(rand()%100+1);
}
v2.push_back(v1);
for(j=0;j<n;j++)//if we dont use this loop every row will be identical
v1.pop_back();
}
}
void aj::show()
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
cout<<v2[i][j]<<" ";
cout<<endl;
}
}
int main()
{
aj a;
a.input();
a.show();
system("pause");
return 0;
}
Why does the class cotain vector v1 if it is used only in function input for initialization vector v2?
It is stupid to use each time the following loop
for(j=0;j<n;j++)//if we dont use this loop every row will be identical
v1.pop_back();
}
Did you hear something about such member functio as clear()? Moreover if vector v1 will be a local object in function input you would not need to call even v1.clear().
What will occur if function input will be called consequently several times?
So your design of he class is very and very bad.
Also I advice you to read at least the description of the member function reserve()