Unordered_map迭代器抛出错误

Unordered_map iterator throwing error

本文关键字:出错 错误 迭代器 map Unordered      更新时间:2023-10-16

我有这样的代码:

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;    
}

我得到无效操作数的错误,无法找出我做错了什么。我试过切换迭代器,但它也可能是我的编译器。我用这个来编译:

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

我的错误是:期望';'在声明结束Unordered_map::const_iterator index = map.find(comp);

间接要求指针操作数('int'无效)__table_.__insert_unique (* __first);

函数模板特化'std::__1::unordered_map, std::__1::equal_to,Std::__1::allocator>>::insert' requested here

任何见解/帮助将不胜感激!

编辑:

我已经返回并修复了错误。

您漏掉了下面语句中的::

unordered_map<long,long>const_iterator
应:

unordered_map<long,long>::const_iterator
相关文章: