insertions into c++ map STL container is failing -


please see following code understand question better.

class comparebyvalue {                                                                                public:                                                                                               bool operator()(const string* s1, const string* s2) const                                           {                                                                                                     if (s1 == s2)                                                                                         return true;                                                                                      if ((s1==null) || (s2==null))                                                                         return false;                                                                                     return (0 == s1->compare(s2->c_str()));                                                           }                                                                                                                                                                                              };   map<string*, string*, comparebyvalue> nodeidtoipaddress;  (int = 0; < nrec; ++i) {   nodeidtoipaddress[ptr1[i]] = ptr2[i];                                                 cout << "added " << *(ptr1[i]) << " , " << *(ptr2[i]) << endl; }  cout << "map has " << nodeidtoipaddress.size() << " elements!" << endl; 

i have map maintains key,value pairs pointers string objects. i'm sure neither keys nor values null pointers. when run above program (well, skipped surrounding code make easier understand), "added ... ..." gets printed 49 times. ptr1[i], ptr2[i] pointers string objects , not null pointers because program doesn't segfault.

the problem have is, when print size of map @ end, says map has 1 element in it.

i appreciate if can give me directions find fix. in advance.

edit: @mark solution worked charm me. thanks

edit2: after seeing valuable feedback @mark , @james, think don't need store pointers strings in map. i'm going change code store strings key/values means don't need custom comparator functor. lot.

your comparison operator isn't right. long can assume no nulls added, can throw if detect any:

bool operator()(const string* s1, const string* s2) const                                       {                                                                                                 if (!s1 || !s2) throw std::runtime_error("whatever");    return *s1 < *s2; }                                                                                  

edit: should have elaborated on reasoning here bit more. comparison operation pass std::map (or set or sort example) must satisfy strict weak ordering. means x op x false, if x op y true y op x false, , x op y, y op z implies x op z (as other rules regarding items can't compared).

your function didn't satisfy in several ways: if (s1 == s2) needs return false. if ((s1==null) || (s2==null)) can't return false because imply null < anything , anything < null. you'd have split , return true s1 null , false s2 null. return (0 == s1->compare(s2->c_str())); incorrect because it's three-way result rather < relation.

since specified nulls never inserted, simplest resolution seemed to throw exception in case (if either null) , thenuse string built-in operator.

all being said now, consider not storing strings pointer in containers unless have actual evidence it's performance bottleneck. simpler design easier maintain , keep correct, tends lead better programs long-term.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -