Question

I have this code:

int solution(int K, const vector<int> &A) {
  int count=0,size,comp=0;
  unordered_map<long,long> map;

  size = A.size();
  if(size==0)
      return 0;

  for(int i=0;i<size;i++){
      map[A[i]] = i;
  }

  for(int i=0;i<size;i++){
      comp = K-A[i];
      unordered_map<long,long>::const_iterator index = map.find(comp); //error here
      if(index == map.end())
          continue;
      else{
          count++;
      }
  }
  cout << "final count: " << count << endl;
  return count;    
}

I'm getting an error for invalid operands and am unable to figure out what I'm doing wrong. I've tried switching iterators but it also might be my compiler. I'm using this to compile:

clang++ -stdlib=libc++ -std=gnu++11 workingpairs.cpp

My errors are: expected ';' at end of declaration unordered_map::const_iterator index = map.find(comp);

indirection requires pointer operand ('int' invalid) __table_.__insert_unique(*__first);

in instantiation of function template specialization 'std::__1::unordered_map, std::__1::equal_to, std::__1::allocator > >::insert' requested here

Any insight/help would be appreciated!

EDIT:

I've gone back and fixed the error.

Was it helpful?

Solution

You missed :: in below statement

unordered_map<long,long>const_iterator

should be:

unordered_map<long,long>::const_iterator
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top