1

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.

3
  • 2
    This would be a good place to use auto index = map.find(comp); Commented May 13, 2014 at 3:41
  • @Blastfurnace instead of the const_interator? or in addition to it? Commented May 13, 2014 at 3:47
  • I would just use the auto keyword. The compiler already knows the type of the expression map.find(comp) so it will declare index as that type. No more typos on those tedious iterator declarations. Commented May 13, 2014 at 3:52

1 Answer 1

2

You missed :: in below statement

unordered_map<long,long>const_iterator

should be:

unordered_map<long,long>::const_iterator
Sign up to request clarification or add additional context in comments.

2 Comments

Wow... thanks! I still have another error: in instantiation of function template specialization 'std::__1::unordered_map<long, long, std::__1::hash<long>, std::__1::equal_to<long>, std::__1::allocator<std::__1::pair<const long, long> > >::insert<int>' requested here map.insert(A[i],i);. Do you know what that is?
that's because map.insert(A[i],i);, insert can take pair, not key/value directly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.