Whats wrong in this code? Please Help


#1

It gives exact output in run as a custom input but shows the wrong output while I go to submit.

vector<pair<int, int>> v;
int cap =0;
LRUCache::LRUCache(int capacity) {

    cap =capacity;

}

int LRUCache::get(int key) {
    
    if(cap ==1 && v.size() ==0)
    {
       
        return -1;
    }
    
    
    
  for(int i =0 ; i<cap ; i++)
  {
      if(v[i].first == key)
      {
          return v[i].second;
        }
        
  }

   return -1;

}

void LRUCache::set(int key, int value) {

  if(v.size()<=cap)
  {
     v.push_back(make_pair(key,value)); 
  }
  if(v.size()>cap)
  {
      v.erase(v.begin());
      v.push_back(make_pair(key,value)); 
  }

}

#2

vector<pair<int,int>> v is global, try using v.clear() in initialization method of LRUCache, it will work