Advertisement
Imran_Mohammed

Everything_of_pair

Jan 25th, 2021
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1.        // In The Name Of Allah
  2.  
  3. #include<bits/stdc++.h>
  4. #include<string.h>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     //Pair has two value and two data type
  10.  
  11.     //Pair Declare :
  12.     pair <string , int >p;
  13.     p.first = "imran";
  14.     p.second = 12;
  15.     cout << p.first << " " << p.second << endl;//Output: imran 12
  16.  
  17.     //User Input :
  18.     pair <string , int >p;
  19.     cin >> p.first >> p.second;
  20.     cout << p.first << " " << p.second;
  21.  
  22.     //Make pair using make_pair function :
  23.     pair<int , int>p;
  24.     p = make_pair(15,19);
  25.     cout << p.first << " " << p.second << endl;//output 15 19
  26.     p.second++;//Increasing
  27.     cout << p.first << " " << p.second << endl;//output 15 20
  28.  
  29.     //Making pair without make_pair :
  30.     pair<string ,vector<int> >p;
  31.     p = {"Imran" , {1,2,3,4,5} };
  32.     cout << p.first << " " << p.second.size() << endl;//Imran 5
  33.  
  34.     //Compare Pair :Fist value check
  35.     //if first value equal then compare second value
  36.     pair < int , int> p1,p2;
  37.     p1 = {2,5};
  38.     p2 = {3,1};
  39.     if(p1 < p2)cout <<"Yes" << endl;//Yes
  40.  
  41.     //Minimum or Maximum pair :
  42.     pair < int, int>p1,p2;
  43.     p1 = {3,5};
  44.     p2 = {1,9};
  45.     pair<int, int>p= max(p1,p2);
  46.     cout << p.first << " " << p.second << endl;//3 5
  47.  
  48.     //Sorting Vector Of pair:
  49.     vector< pair<int, int> >v;
  50.     v.push_back( {6,5} );
  51.     v.push_back( {2,3} );
  52.     v.push_back( {4,5} );
  53.     v.push_back( {6,1} );
  54.     v.push_back( {1,9} );
  55.     sort(v.begin() , v.end());
  56.     for(auto u:v)cout <<u.first << " " << u.second << endl;
  57.     /*1 9
  58.     2 3
  59.     4 5
  60.     6 1
  61.     6 5*/
  62.  
  63.     //Sorting pair of array:
  64.     pair<int , int> p[ ] = { {6,5}, {2,3}, {4,5}, {6,1}, {1,9} };
  65.     sort(p, p+5);
  66.     for(int i=0; i<5; i++){
  67.         cout << p[i].first << " " << p[i].second << endl;
  68.     }
  69.     /*1 9
  70.     2 3
  71.     4 5
  72.     6 1
  73.     6 5*/
  74.  
  75.     //Unique Vector of pair:
  76.     vector< pair< string , int> >v;
  77.     v.push_back( {"shariar" , 21} );
  78.      v.push_back( {"momo" , 13} );
  79.       v.push_back( {"sharif" , 34} );
  80.        v.push_back( {"shariar" , 35} );
  81.         v.push_back( {"sharif" , 34} );
  82.          v.push_back( {"shariar" , 21} );
  83.           v.push_back( {"momo" , 13} );
  84.           sort(v.begin(), v.end());
  85.           int sz = unique(v.begin(), v.end())- v.begin();
  86.           for(int i=0; i<sz; i++){
  87.             cout << v[i].first << " " << v[i].second << endl;
  88.           }
  89.           /*momo 13
  90.           shariar 21
  91.           shariar 35
  92.           sharif 34*/
  93.          
  94.          
  95.     return 0;
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement