Advertisement
Shuva_Dev

Everything about pair

Dec 16th, 2022
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1.  
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6.  
  7. bool cmp ( const pair<int,int> &p1, const pair<int, int> &p2  )
  8. {
  9.     if ( p1.first > p2.first ) return 1;
  10.     if ( p1.first == p2.first ) return ( p1.second < p2.second );
  11.     return 0;
  12. }
  13.  
  14. int main()
  15. {
  16.  
  17.     /// Declear a pair o integers
  18.  
  19.     pair<int,int> p;
  20.  
  21.     p = make_pair ( 2, 3 );
  22.     cout << p.first << " " << p.second << endl; /// 2 3
  23.  
  24.     p.first++;
  25.     cout << p.first << " " << p.second << endl; /// 3 3
  26.  
  27.     pair<int, int> p1 = { 2, 3 };
  28.     pair<int, int> p2 = { 1, 6 };
  29.  
  30.     /// Getting minimum of 2 pairs
  31.     p = min ( p1, p2 );
  32.     cout << p.first << " " << p.second << endl; /// 1 6
  33.  
  34.     /// Getting maximum of 2 pairs
  35.     p = max ( p1, p2 );
  36.     cout << p.first << " " << p.second << endl; /// 2 3
  37.  
  38.     /// Sorting pair of integers
  39.  
  40.     vector<pair<int,int>> v;
  41.     v.push_back ( { 1, 5 } );
  42.     v.push_back ( { 2, 5 } );
  43.     v.push_back ( { 7, 1 } );
  44.     v.push_back ( { 3, 6 } );
  45.     v.push_back ( { 3, 6 } );
  46.     v.push_back ( { 7, 1 } );
  47.  
  48.     sort ( v.begin(), v.end() );
  49.     for ( auto u : v ) cout << u.first << " " << u.second << endl;
  50.     cout << endl;
  51.     /**
  52.     1 5
  53.     2 5
  54.     3 6
  55.     3 6
  56.     7 1
  57.     7 1
  58.  
  59.     */
  60.  
  61.     /// Making unique pair of integers
  62.  
  63.     int Sz = unique ( v.begin(), v.end() ) - v.begin();
  64.     cout << Sz << endl;
  65.     for ( int i = 0; i < Sz; i++ ) cout << v[i].first << " " << v[i].second << endl;
  66.     cout << endl;
  67.  
  68.     /**
  69.     4
  70.     1 5
  71.     2 5
  72.     3 6
  73.     7 1
  74.  
  75.     */
  76.  
  77.     /// sorting using comparator
  78.     v = { {2, 3}, {4, 5}, {1, 5}, {1, 6}, {6, 7}, {6, 8} };
  79.  
  80.     sort ( v.begin(), v.end(), cmp );
  81.     for ( auto u : v ) cout << u.first << " " << u.second << endl;
  82.     cout << endl;
  83.  
  84.     /**
  85.  
  86.     6 7
  87.     6 8
  88.     4 5
  89.     2 3
  90.     1 5
  91.     1 6
  92.  
  93.     */
  94.  
  95.  
  96.     v = { {2, 3}, {4, 5}, {1, 5}, {1, 6}, {6, 7}, {6, 8} };
  97.  
  98.     for ( int i = 0; i < v.size(); i++ ) v[i].first *= -1;
  99.     sort ( v.begin(), v.end() );
  100.     for ( auto u : v ) cout << (u.first*-1) << " " << u.second << endl;
  101.     cout << endl;
  102.  
  103.     /**
  104.  
  105.     6 7
  106.     6 8
  107.     4 5
  108.     2 3
  109.     1 5
  110.     1 6
  111.  
  112.     */
  113.  
  114.  
  115.     return 0;
  116. }
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement