fork download
  1. #include <iostream>
  2. #include<vector>
  3. #include<map>
  4. using namespace std;
  5.  
  6. vector<int>vec[10];
  7. vector<int>vec1[10];
  8. map<pair<int,int>,int >m; // maps are nothing but just maps a key to a value
  9. map<pair<int,int>,int >:: iterator it;
  10.  
  11. int main() {
  12. // your code goes here
  13. int ele,i,j,row;
  14.  
  15. for(i=0;i<3;i++)
  16. {
  17. for(j=0;j<3;j++)
  18. {
  19. cin>>ele;
  20. vec[i].push_back(ele);
  21. vec1[i].push_back(ele); // see further to know why this has been done
  22. }
  23. }
  24.  
  25. // taking coloumn 1
  26.  
  27. for(row=0;row<3;row++)
  28. {
  29. m.insert(make_pair(make_pair(vec[row][1],row),row));
  30. }
  31.  
  32. for(it=m.begin(); it!=m.end(); ++it)
  33. {
  34. cout<<(it->first).first<<" "<<(it->first).second<<endl; // this prints all the values of the desired coloumn
  35. // notice the values are sorted
  36. }
  37.  
  38. it=m.begin();
  39.  
  40. for(row=0;row<3;row++)
  41. {
  42. // desired row is (it->first).second;
  43. int des= (it->first).second;
  44. for(j=0;j<3;j++)
  45. {
  46. vec[row][j]= vec1[des][j];
  47. }
  48. ++it;
  49. }
  50.  
  51. cout<<"SORTED ORDER : \n";
  52.  
  53. for(i=0;i<3;i++)
  54. {
  55. for(j=0;j<3;j++)
  56. cout<<vec[i][j]<<" ";
  57.  
  58. cout<<endl;
  59.  
  60. }
  61.  
  62. return 0;
  63. }
Success #stdin #stdout 0s 3480KB
stdin
7 8 9
1 2 3
4 5 6
stdout
2 1
5 2
8 0
SORTED ORDER : 
1 2 3 
4 5 6 
7 8 9