samedi 2 mai 2020

Why and when do we add pointer in front of the lowerbound function in c++?

So, I was solving a question and wrote this code:

#include <bits/stdc++.h>
using namespace std;

int main()
{
   int t;
   cin >> t;

   while (t--)
   {
      int n, x;
      cin >> n;
      map <int, vector<int> > m;

      for (int i = 0; i < n; i++)
      {
         cin >> x;
         m[x].push_back(i);
      }
      int prev_ind = n;
      int ans = 0;

      for (auto i : m)
      {
         if (i.second.back() < prev_ind)
         {
            ans++;
            prev_ind = i.second[0];
         }
         else
            prev_ind = *lower_bound(i.second.begin(), i.second.end(), prev_ind);
      }
      cout << ans << endl;
   }
}

So, when I remove the pointer from the front of lower_bound function, the code shows compilation error, can anybody tell me why this happens?

Aucun commentaire:

Enregistrer un commentaire