我有这个代码:
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.insert(A [i],i);
任何人都可以解释我发生了什么事吗?
还使用它来编译:clang -stdlib = libc -std = gnu 11 workingpairs.cpp
3 回答
std :: unordered_map :: insert需要一对:
双参数版本采用迭代器,您不需要这里 .
std::unordered_map::insert的两个参数形式要么是一对迭代器(用于插入范围),要么是迭代器(用作插入元素的位置的提示)和元素 .
您应该使用
std::pair
在特定键处插入值:您在
map.insert(A[i],i)
上的错误是因为它要求您插入容器的value_type
(key/value
对) . 您正在使用两个参数调用insert()
,并且唯一匹配的重载不是您在此情况下所需的重载 .你可以说:
要么
要么