Advertisement
ANTAR_NANDI

STL Pair

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