我有这个代码:
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/