Just declare the vector as:
vector<datatype>v[size]
Then, you may push like:
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
cin>>ele;
v[i].push_back(ele);
}
}
Similarly, other operations:
cout<<"Sorting v[2] \n";
sort(dist[2].begin(),dist[2].end());
Finding:
cout<<"Finding element 3 in v[2](0-indexed) : ";
it= find(dist[2].begin(),dist[2].end(),3);
if(it!=dist[2].end())
cout<<"Element found\n";
else
cout<<"Doesn't exist";
Sorting as per some value(something like this, not tested):
int compare(const void * a1, const void * b1)
{
int a= *(int*)(a);
int b= *(int*)(b);
if(a<num && num<b)
return -1;
if(b<num && num<a)
return 1;
return 0;
}
int values[100],idx=0;
for(i=0;i<dist[1].size();i++)
{
values[idx]= dist[1][i];
//cout<<values[idx]<<" ";
idx++;
}
qsort (values, idx, sizeof(int), compare);
For complete working code, refer this. Hope it clears!!
Edit: For sorting as per the colomn value, lets say we have:
3 5 5 6
2 2 5 8
3 1 4 5
8 7 6 4
what we can do is, make a map having pairs as <element, row number>
. In this case, what we have in map is {(5,1),(2,2),(1,3),(7,4)}
. These entries will be inserted in the map in the sorted order(logarithmic time complexity) of the first value of the pair. The map will then be: {(1,3),(2,2),(5,1),(7,4)}
. As per the entries just change the rows values and it’s done.