gpt4 book ai didi

c++ - unordered_map 找不到 key

转载 作者:太空宇宙 更新时间:2023-11-04 13:59:11 37 4
gpt4 key购买 nike

我在我的程序中使用了 unorder_map。问题是有时找不到我插入 map 的 key ,我不知道原因,以下是我的代码:

class FlowKey: public std::pair<const char*, unsigned int>
{
public:
FlowKey(const char* s, unsigned int l): std::pair<const char*, unsigned int>(s, l) {
fprintf(stderr, "raw string %s len %u, after pair, first %s second %u", s, l, this- >first, this->second);
}
bool operator==(const FlowKey& c) const
{
if (this->second == c.second) {
if (0 == strncmp((char*)this->first, (char*)c.first, this->second)) {
fprintf(stderr, "key compare true\n");
return true;
} else {
fprintf(stderr, "this first ip:%s c first ip:%s\n", (char*)this->first, (char*)c.first);
}
} else {
fprintf(stderr, "this second %d, c second %d\n", this->second, c.second);
}
fprintf(stderr, "key compare false\n");
return false;
}
};

class FlowKeyHash
{
public:
size_t operator()(const FlowKey& that ) const
{
unsigned int h = fnv_32a_buf(that.first, that.second, FNV_32A_INIT);
fprintf(stderr, "ip %s len %u digest %u\n", that.first, that.second, h);
return (size_t) h;
}
};
typedef std::unordered_map<unsigned int, TcpInfo*, EmptyHash> UserAgent_Map;
typedef std::unordered_map<FlowKey, UserAgent_Map, FlowKeyHash> IP_Map;

用法:

FlowKey k(ipString, strlen(ipString));
IP_Map::iterator it = m_iptable.find(k);
if (it == m_iptable.end()) {
//insert new entry to ip table and useragent table
struct timeval time;
gettimeofday(&time, NULL);
fprintf(stderr, "ip not equel, insert: ip:(%s)ip-digest(%u), k first:%s k second %d tablesize %d hashsize %lu\n", ipString, ip_digest, k.first, k.second, m_tableSize, m_iptable.size());
for(it = m_iptable.begin(); it != m_iptable.end(); ++it) {
fprintf(stderr, "key %s len %d\n", it->first.first, it->first.second);
}
++m_tableSize;
} else {
//update ip table
struct timeval time;
gettimeofday(&time, NULL);
UserAgent_Map::iterator it2 = it->second.find(user_agent_digest);
if (it2 == it->second.end()) {
//insert new entry to user_agent table
++m_tableSize;
fprintf(stderr, "user-agent not equel, insert: use-agent-digest(%u)\n", user_agent_digest);
}

程序插入了一对 key (192.168.2.20, 12),有时能找到这个 key ,但偶尔找不到这个 key ,日志显示:

raw key 192.168.2.20 len 12, after pair, first 192.168.2.20 second 12
ip 192.168.2.20 len 12 digest 1737338608
this first ip:192.168.2.20 c first ip:184.28.16.107
key compare false
ip 192.168.2.20 len 12 digest 1737338608
this first ip:192.168.2.20 c first ip:184.28.16.107
key compare false
ip not equel, insert: ip:(192.168.2.20)ip-digest(1737338608), k first:192.168.2.20 k second 12 tablesize 1 hashsize 2
key 192.168.2.20 len 12
key 184.28.16.107 len 12

好奇怪,要查找的key是<192.168.2.20, 12>,而且已经在map里面了,为什么find函数找不到,为什么查找的key变成了184.28.16.107 find() 调用。 find()前后,key是192.168.2.20,find的时候是184.28.16.107,len是12不对,为什么,key<184.28.16.107,12>从哪里来,谁都能找到我对 unordered_map 的使用是错误的?或者程序的逻辑错误,我为此花费了很多时间,但我找不到任何原因。你能帮帮我吗?

最佳答案

看起来很像 WhozCraig 分析的悬空指针问题。您可能正在使用相同的 ipString 两次或三次...

你能把你的对中的“const char*”改成字符串吗?我敢打赌它会解决您的问题。

关于c++ - unordered_map 找不到 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19938165/

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