Advertisement
maycod23

Vector_Functions

Feb 27th, 2022
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. bool cmp(pair<int, int>&p1, pair<int, int>&p2)
  4. {
  5.     if (p1.first == p2.first) return (p1.second >= p2.second);
  6.     return (p1.first < p2.first);
  7. }
  8. int main()
  9. {
  10.  
  11.     //sorting using functions
  12.     //lower_bound and upper_bound
  13.     //
  14.     // maps and sets
  15.  
  16.     // vector<int> v;
  17.     // int n; cin >> n;
  18.     // for (int i = 0; i < n; i++)
  19.     // {
  20.     //  int x; cin >> x;
  21.     //  v.push_back(x);
  22.     // }
  23.     //v n
  24.     // cout << v[0] << endl;
  25.     //iterators
  26.     // cout << *(v.begin()) << endl;//0th index
  27.     // cout << *(v.begin() + 2) << endl;
  28.  
  29.     //end iterator
  30.     // cout << *(v.end()) << endl;
  31.     // sort(v.begin(), v.end());
  32.  
  33.     //l=0,r=(n-1)->for whole vector
  34.     // sort(v.begin() + l, v.begin() + r + 1);
  35.     //sort(v.begin(),v.end())->sorted whole vector
  36.     // for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
  37.  
  38.  
  39.     //you for (1<=i<=n)
  40.     // int l, r; cin >> l >> r; //1 indexing
  41.     // l = l - 1; r = r - 1;//0 indexing
  42.     // sort(v.begin() + l, v.begin() + r + 1);
  43.  
  44.     //O(nlogn)->heap sort and quick sort (T.c)
  45.  
  46.     // for (int i = 0; i < v.size(); i++)
  47.     // {
  48.     //  cout << v[i] << " ";
  49.     // }
  50.  
  51.     // vector<pair<int, int>> v;
  52.     // int n; cin >> n;
  53.     // for (int i = 0; i < n; i++)
  54.     // {
  55.     //  int x, y; cin >> x >> y;
  56.     //  v.push_back(make_pair(x, y));
  57.     // }
  58.     // for (int i = 0; i < n; i++)
  59.     // {
  60.     //  cout << v[i].first << " " << v[i].second << endl;
  61.     // }
  62.  
  63.     // int l, r; cin >> l >> r;//0 indexing
  64.     // sort(v.begin() + l, v.begin() + r + 1);
  65.     // cout << "Vector After Sorting" << endl;
  66.  
  67.     // for (int i = 0; i < n; i++)
  68.     // {
  69.     //  cout << v[i].first << " " << v[i].second << endl;
  70.     // }
  71.  
  72.     //upper-Bound and lower-bound
  73.  
  74.     vector<pair<int, int>> v;
  75.     int n; cin >> n;
  76.     for (int i = 0; i < n; i++)
  77.     {
  78.         int x, y; cin >> x >> y;
  79.         v.push_back(make_pair(x, y));
  80.     }
  81. //sorted
  82.     // sort(v.begin(), v.end());
  83.     //lower_bound->first occurence of x (if x is present)
  84.  
  85.     // int x; cin >> x;
  86.     // auto itr = lower_bound(v.begin(), v.end(), x) - v.begin();
  87.     // cout << itr << endl;
  88.  
  89.     // int itr = upper_bound(v.begin(), v.end(), x) - v.begin();
  90.     // cout << itr << endl;
  91.  
  92.     // for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
  93.     // cout << endl;
  94.     // for (auto i : v)
  95.     // {
  96.     //  cout << i << " ";
  97.     // }
  98.     sort(v.begin(), v.end());
  99.     for (auto i : v)
  100.     {
  101.         cout << i.first << " " << i.second << " " << endl;
  102.     }
  103.     cout << "AFTER USING COMPARATOR FUNCTION" << endl;
  104.     sort(v.begin(), v.end(), cmp);
  105.     for (auto i : v)
  106.     {
  107.         cout << i.first << " " << i.second << " " << endl;
  108.     }
  109.     // 4
  110. // 0 -1
  111. // 1 2
  112. // 0 -2
  113. // 1 3
  114. // 0 -2
  115. // 0 -1
  116. // 1 2
  117. // 1 3
  118. // AFTER USING COMPARATOR FUNCTION
  119. // 0 -1
  120. // 0 -2
  121. // 1 3
  122. // 1 2
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement