gpt4 book ai didi

c++ - 在 cpp unordered_map 的自定义哈希函数中插入不起作用

转载 作者:行者123 更新时间:2023-11-28 02:09:45 25 4
gpt4 key购买 nike

<分区>

我正在尝试使用以下构造的 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.

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com