简体   繁体   English

参赛作品( <key,values> )未输入到地图中

[英]Entries(<key,values>) are not entered in a map

I want save values in a std::map if an hash function return a value that isn't already contained in the map. 如果哈希函数返回映射中尚未包含的值,我想将值保存在std::map map中。 The data structure is: 数据结构为:

std::map<uint8_t* , MyObject* , lex_compare> mymap;

where uint8_t* point to a C-style array of 128 bit ( uint8_t hash_value[16] ) that contain the hash function applied to a field of class MyObject . 其中uint8_t*指向一个128位C样式数组( uint8_t hash_value[16] ),其中包含应用于MyObject类的字段的哈希函数。 I use lex_compare for a personalized comparison: 我使用lex_compare进行个性化比较:

struct lex_compare {
bool operator() (const uint8_t *hash1, const uint8_t *hash2) const {
    /*for(int i=0 ; i<16 ; i++)
             {
                 cout<<(unsigned int)hash1[i]<<" ";
             }
             cout<<endl<<endl;
             for(int i=0 ; i<16 ; i++)
             {
                 cout<<(unsigned int)hash2[i]<<" ";
             }
    int m=memcmp(hash2,hash1,16);
    cout<<"m is è"<<m<<endl;*/
    return (memcmp(hash2,hash1,16)<0); //compare 16 byte.
    }
};

To ensure insertion only if an hash value is not already contained in the map I use: 为了确保仅在映射中尚未包含哈希值时才插入,我使用:

while(mymap.size()<R)
{
myObject *temp_o  = new myObject(parameters);
uint8_t hash_result = my_hash_function(myObject->return_field()) // return_field is a specific field of myObject 
mymap.insert(make_pair(hash_result,temp_o));
}

But only one element is inserted in mymap , thus I go in endless loop. 但是只有一个元素插入到mymap ,因此我陷入了无限循环。 Why? 为什么? I can not explain it. 我无法解释。 Look at lex_compare I saw that this function return always zero value (because is called on 2 equals elements). lex_compare我看到此函数始终返回零值(因为在2个equals元素上调用了该函数)。 May be is a trivial problem, but I'm not able to see it. 可能是一个小问题,但我看不到它。

EDIT: I correct problem in comparison function. 编辑:我比较功能中的问题。 But later correction main problem remains 但是后来纠正主要问题仍然存在

Short answer: try with 简短答案:尝试

return (memcmp(hash2,hash1,16) > 0);

Long answer: memcpy() return a negative value if the first argument is lower than the second, zero if they are equal and a positive value if the first value is greater than the second. 长答案:如果第一个参数小于第二个参数,则memcpy()返回负值;如果相等,则返回零;如果第一个参数大于第二个参数,则返回正值。

But your operator() return a boolean. 但是您的operator()返回一个布尔值。 So negative and positive value are converted in true and zero is converted in false . 因此,将负值和正值转换为true ,将零转换为false So, according your operator() , hash1 is lower that hash2 if they are different. 因此,根据您的operator() ,如果hash1hash2不同,则它们比hash1低。 And the bad part is that, if hash1 and hash2 are different, hash1 result lower than hash2 and hash2 result lower than hash1 . 糟糕的是,如果hash1hash2不同,则hash1结果低于hash2 hash2结果低于hash1 This give to the program an undefined behavior. 这给程序带来了不确定的行为。

Solution: modify the return instruction or in this way 解决方法:修改返回指令或以这种方式

return (memcmp(hash2,hash1,16) > 0);

or as follows 或如下

return (memcmp(hash2,hash1,16) < 0);

to be sure that true == operator()(hash1, hash2) imply that false == operator()(hash2, hash1) 确保true == operator()(hash1, hash2)表示false == operator()(hash2, hash1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM