c++ - Using mpz_t as key for std::map -


i trying build map mpz_t keys uint values. don't know why mpz_t keys can somehow not looked in map.

mpz_t leftsidevalues[1 << 20];  int main() {     std::map<mpz_t, uint> leftside;      (uint = 0; < 1 << 20; i++)     {         mpz_init(leftsidevalues[i]);          // compute stuff here...          // save computed value our map         leftside[leftsidevalues[i]] = i;          // lookup see whether our value can found         std::cout << leftside.at(leftsidevalues[i]) << " -- " << << std::endl;     }      return 0; } 

the expected output lot of lines looking "0 -- 0", "1 -- 1" etc. not happen. instead:

terminate called after throwing instance of 'std::out_of_range'   what():  map::at

is there other step need take make mpz_t usable in map?

it seems map cannot compare 2 mpz_t instances.

according the c++ reference maps implemented binary search trees. therefore if elements cannot compared search impossible.

adding comparer fixed problem:

struct mpzcompare {     bool operator() (const mpz_t val1, const mpz_t val2) const     {         return mpz_cmp(val1, val2) > 0;     } };  std::map<mpz_t, uint, mpzcompare> leftside; 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -