【发布时间】:2020-06-30 07:28:17
【问题描述】:
我正在尝试创建一个哈希表,其中包含一个由结构组成的向量内的向量。 v[1].push_back(value); 它给了我一个错误:
error C2664: 'void std::vector<node,std::allocator<node>>::push_back(_Ty &&)': cannot convert argument 1 from 'int' to 'const _Ty &'
with
[
_Ty=node
]
note: Reason: cannot convert from 'int' to 'const _Ty'
with
[
_Ty=node
]
note: No constructor could take the source type, or constructor overload resolution was ambiguous
这是我的代码: 结构节点{ 整数数据;
node() {
data = 0;
}
};
class hashmap {
public:
vector<vector<struct node>> v;
vector<struct node> n;
hashmap() {
for (int i = 0; i < 5; i++) {
v.push_back(n);
}
}
void insert(int key, int value) {
int index = Hashfunction(key);
v[1].push_back(value);
}
int Hashfunction(int key) {
int index = key % v.size();
return index;
}
};
【问题讨论】:
-
将节点的 ctor 更改为:
node(int d = 0) { data = d; }。现在,您没有从int到node结构的转换。 -
@rafix07 事件进一步,
node( int d = 0 ) : data { d } {}
标签: c++ hash stl std push-back