1

プログラムで 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

とても奇妙です。検索対象のキーは <192.168.2.20, 12> で、それはマップにありました。なぜ find 関数がそれを見つけられないのか、なぜ find( ) 電話。find() の前後で、キーは 192.168.2.20 で、検索中は 184.28.16.107 で、len は 12 です 正しくありません、なぜ、そしてキー <184.28.16.107,12> はどこから、誰でも見つけることができますunordered_map の使い方が間違っていますか? またはプログラムの論理エラー、これには多くの時間がかかりましたが、理由が見つかりません。手伝って頂けますか?

4

1 に答える 1