我正在尝试使用以下 unordered_map 结构。
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <string>
#include <unordered_map>
using namespace std;
int width = 6;
int length = 6;
string parameters[] = {"0 0 0 1","6 6 5 6"};
struct co_ord {
int x,y;
int x1, y1;
co_ord(int x, int y, int x1, int y1) : x(x), y(y), x1(x1), y1(y1) {}
};
struct co_ordHash {
size_t operator()(const co_ord& key) const {
return ((10+key.x)*100000000) + ((10+key.y)*1000000) + ((10+key.x1)*10000) + ((10+key.y1)*100);
}
};
struct co_ordEqual {
bool operator()(const co_ord& lhs, const co_ord& rhs) const {
return lhs.x == rhs.x && lhs.y == rhs.y && lhs.x1 == rhs.x1 && lhs.y1 == rhs.y1;
}
};
int maxPath(int x, int y, unordered_map<co_ord, bool, co_ordHash, co_ordEqual> barriers) {
// unordered_map<co_ord, bool>::const_iterator iter;
int sum = 0;
if (x > width || y > length) {
return 0;
}
if (x==width && y==length) {
return 1;
}
co_ord *temp;
temp = new co_ord(x, y, x+1, y);
unordered_map<co_ord, bool, co_ordHash, co_ordEqual>::const_iterator iter1 = barriers.find(*temp);
if (iter1 == barriers.end()) {
sum = maxPath(x+1, y, barriers);
}
temp = new co_ord(x, y, x, y+1);
unordered_map<co_ord, bool, co_ordHash, co_ordEqual>::const_iterator iter2 = barriers.find(*temp);
if (iter2 == barriers.end()) {
return sum + maxPath(x, y+1, barriers);
}
return sum;
}
int main(int argc, char const *argv[]) {
string barrier[4];
string delimiter = " ";
co_ord *temp;
unordered_map<co_ord, bool, co_ordHash, co_ordEqual> barriers;
int i,j,k, para_len = sizeof(parameters)/sizeof(parameters[0]);
for (i = 0; i < para_len; i++) {
j = k = 0;
while (j<4) {
barrier[j] = "";
while (parameters[k] != " " || parameters[k]!= "\n") {
barrier[j] += parameters[k];
k++;
}
barrier[j] += '\n';
j++;
}
temp = new co_ord(stoi(barrier[0]), stoi(barrier[1]), stoi(barrier[2]), stoi(barrier[3]));
barriers.insert(temp, false); // here is the problem
}
cout<<maxPath(0,0, barriers);
return 0;
}
我无法插入函数。任何人都可以帮助我吗? 我收到这个错误:
avoidroads_dp.cc:79:14: error: no matching member function for call to 'insert'
barriers.insert(temp, false);
~~~~~~~~~^~~~~~
1 error generated.
答案 0 :(得分:2)
使用std :: make_pair插入数据。还有一件事:根据您的模板签名,您使用co_ord类型作为键,但您尝试添加指向co_ord对象的指针。您应该使用指针作为键来更改签名或拒绝。
这是一个固定字符串的例子
barriers.insert(make_pair(*temp, false));