unordered_map找不到密钥

时间:2013-11-12 19:27:39

标签: c++ c++11

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

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);
            } 
程序已插入一对密钥对(192.168.2.20,12),有时可以找到这个密钥,但偶尔找不到这个密钥,日志显示:

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

这很奇怪,要搜索的密钥是&lt; 192.168.2.20,12&gt;,它已经在地图中,为什么find函数找不到它,为什么搜索到的密钥变为184.28.16.107在find()调用期间。在find()之前和之后,密钥是192.168.2.20,在查找期间,它是184.28.16.107,而len是12不对,为什么,以及密钥&lt; 184.28.16.107,12&gt;从,任何人都可以找到我对unordered_map的使用是错误的?或程序的逻辑错误,我花了很多时间,但我找不到任何理由。你能救我吗?

1 个答案:

答案 0 :(得分:0)

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

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