c++ - Unordered_map 散列间接需要指针操作数

标签 c++ c++11 unordered-map

我有这个代码:

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.insert(A[i],i); //error here
  }

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

似乎无法弄清楚为什么它会提示。错误看起来像这样:

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

在函数模板特化的实例化 'std::__1::unordered_map, std::__1::equal_to, std::__1::allocator >>::insert' 此处请求 map .插入(A[i],i);

谁能给我解释一下这是怎么回事吗?

也用它来编译:clang++ -stdlib=libc++ -std=gnu++11workingpairs.cpp

最佳答案

您在 map.insert(A[i],i) 上的错误是因为它希望您插入容器的 value_type (一个 key/值 对)。您正在使用两个参数调用 insert(),并且唯一匹配的重载不是您在本例中想要的重载。

你可以说:

map[A[i]] = i;

map.insert(std::make_pair(A[i], i));

map.emplace(A[i], i);

关于c++ - Unordered_map 散列间接需要指针操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23622650/

相关文章:

c++ - Windows - 在 C++ 中读取鼠标的 dpi 设置

c++ - 为什么我应该得到 NaN 而得到 NaN?

c++ - C++14如何提高 "str1 + str2 + str3 + ..."的效率?

c++ - 为什么 std::make_tuple(7 + N...) 在 C++11 中是合法的?

C++:在 unordered_map<UINT_PTR,Object*> 中查找修改后的 UINT_PTR

C++将文本写入文件,如何多行

在 Linux 机器上使用 cmake Qt5 Webkit 进行 C++ 交叉编译,目标是使用 mingw 的 Windows,失败

c++ - 关于 std::thread 中的 C++ 自动类型转换行为

c++ - 无序多重映射查找所有值

c++ - 通过其他字符串索引字符串