fork download
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<stdlib.h>
  4. #include<vector>
  5. #include<map>
  6. using namespace std;
  7.  
  8. vector<int>dist[42];
  9. vector<int>:: iterator it;
  10.  
  11. bool compare(int a, int b)
  12. {
  13. if(a<b)
  14. return 1;
  15.  
  16. return -1;
  17. }
  18.  
  19. int main()
  20. {
  21. int i,j,ele;
  22. for(i=0;i<3;i++)
  23. {
  24. for(j=0;j<3;j++)
  25. {
  26. cin>>ele;
  27. dist[i].push_back(ele);
  28. }
  29. }
  30. cout<<"Finding element 3 in v[2](0-indexed) : ";
  31.  
  32. it= find(dist[2].begin(),dist[2].end(),3);
  33.  
  34. if(it!=dist[2].end())
  35. cout<<"Element found\n";
  36.  
  37. else
  38. cout<<"Doesn't exist";
  39.  
  40. cout<<"Sorting v[2] \n";
  41.  
  42. sort(dist[2].begin(),dist[2].end());
  43.  
  44. for(i=0;i<3;i++)
  45. {
  46. for(j=0;j<3;j++)
  47. {
  48. cout<<dist[i][j]<<" ";
  49. }
  50.  
  51. cout<<endl;
  52. }
  53.  
  54. cout<<"as per the specific index (lets say sort v[1] with some element 3): \n";
  55.  
  56. sort(dist[1].begin(), dist[1].end(),compare);
  57.  
  58. for(i=0;i<3;i++)
  59. {
  60. for(j=0;j<3;j++)
  61. {
  62. cout<<dist[i][j]<<" ";
  63. }
  64.  
  65. cout<<endl;
  66. }
  67. return 0;
  68. }
Success #stdin #stdout 0s 3480KB
stdin
1 2 3
4 5 2
7 8 3
stdout
Finding element 3 in v[2](0-indexed) : Element found
Sorting v[2] 
1 2 3 
4 5 2 
3 7 8 
as per the specific index (lets say sort v[1] with some element 3): 
1 2 3 
2 5 4 
3 7 8