By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,002 Members | 1,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,002 IT Pros & Developers. It's quick & easy.

map & iterator

P: n/a
Hello
I have strange problem, i have map<myclass1*,myclass2*,Compare>.
Code:
std::map<myclass1*,myclass2*,Compare> m=mymap;
std::map<myclass1*,myclass2*,Compare>::const_itera tor ci;

printf("Elements: %d\n",m.size());
for(ci=m.begin();ci!=m.end();ci++){
printf("text\n");
}

The problem is that i receive:
Elements: 5
text
text

So i have added 5 elements to my map, but iterator iterates only thru
two of them. Where could be the problem ?

Thanx
Michal

Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
vertigo wrote:
Hello
I have strange problem, i have map<myclass1*,myclass2*,Compare>.
Code:
std::map<myclass1*,myclass2*,Compare> m=mymap;
std::map<myclass1*,myclass2*,Compare>::const_itera tor ci;

printf("Elements: %d\n",m.size());
for(ci=m.begin();ci!=m.end();ci++){
printf("text\n");
}

The problem is that i receive:
Elements: 5
text
text

So i have added 5 elements to my map, but iterator iterates only thru
two of them. Where could be the problem ?


Maybe your Compare is broken? What does it do?

Yevgen
Jul 22 '05 #2

P: n/a

"vertigo" <ax***@wp.pl> wrote in message
news:ci**********@nemesis.news.tpi.pl...
Hello
I have strange problem, i have map<myclass1*,myclass2*,Compare>.
Code:
std::map<myclass1*,myclass2*,Compare> m=mymap;
std::map<myclass1*,myclass2*,Compare>::const_itera tor ci;

printf("Elements: %d\n",m.size());
for(ci=m.begin();ci!=m.end();ci++){
printf("text\n");
}

The problem is that i receive:
Elements: 5
text
text

So i have added 5 elements to my map, but iterator iterates only thru
two of them. Where could be the problem ?

Thanx
Michal


That is a weird problem. The way to solve these problems is to post a
complete program here. Then dozens of willing volunteers will compile and
test your code and give you the answer pronto.

John
Jul 22 '05 #3

P: n/a
> Maybe your Compare is broken? What does it do?

YES. When i declare/use map without Compare everything works fine.
My compare:
struct Compare{
bool operator()(sha1 *sha1, sha1 *sha2) const{
char *s1;
char *s2;
s1=sha1_sprintf_hex(sha1);
s2=sha1_sprintf_hex(sha2);
if (strncmp(s1,s2,40)==0){
return false;
}
return true;
}
};

Where sha1 object represents 160bit SHA1 value, sha1_sprintf_hex(sha)
returns pointer to char table (nice formatted SHA1 string).

What could be wrong with that ?

Thanx
Michal
Jul 22 '05 #4

P: n/a

"vertigo" <ax***@wp.pl> wrote in message
news:ci**********@nemesis.news.tpi.pl...
Maybe your Compare is broken? What does it do?


YES. When i declare/use map without Compare everything works fine.
My compare:
struct Compare{
bool operator()(sha1 *sha1, sha1 *sha2) const{
char *s1;
char *s2;
s1=sha1_sprintf_hex(sha1);
s2=sha1_sprintf_hex(sha2);
if (strncmp(s1,s2,40)==0){
return false;
}
return true;
}
};

Where sha1 object represents 160bit SHA1 value, sha1_sprintf_hex(sha)
returns pointer to char table (nice formatted SHA1 string).

What could be wrong with that ?


Because compare should test for less than not equality.

struct Compare{
bool operator()(sha1 *sha1, sha1 *sha2) const{
char *s1;
char *s2;
s1=sha1_sprintf_hex(sha1);
s2=sha1_sprintf_hex(sha2);
return strncmp(s1,s2,40) < 0;
}
};

john
Jul 22 '05 #5

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2r*************@uni-berlin.de...

"vertigo" <ax***@wp.pl> wrote in message
news:ci**********@nemesis.news.tpi.pl...
Maybe your Compare is broken? What does it do?


YES. When i declare/use map without Compare everything works fine.
My compare:
struct Compare{
bool operator()(sha1 *sha1, sha1 *sha2) const{
char *s1;
char *s2;
s1=sha1_sprintf_hex(sha1);
s2=sha1_sprintf_hex(sha2);
if (strncmp(s1,s2,40)==0){
return false;
}
return true;
}
};

Where sha1 object represents 160bit SHA1 value, sha1_sprintf_hex(sha)
returns pointer to char table (nice formatted SHA1 string).

What could be wrong with that ?


Because compare should test for less than not equality.

struct Compare{
bool operator()(sha1 *sha1, sha1 *sha2) const{
char *s1;
char *s2;
s1=sha1_sprintf_hex(sha1);
s2=sha1_sprintf_hex(sha2);
return strncmp(s1,s2,40) < 0;
}
};


Also the way you are using sha1_sprintf_hex looks dubious. If you are
returning a pointer to a dynamically allocated string then you have a memory
leak, if you are returning a pointer to a staticly allocated array then you
have a bug. Perhaps you should post the code for sha1_sprintf_hex.

john
Jul 22 '05 #6

P: n/a
When i always return with false:
struct Compare{
bool operator()(sha1 *sha1, sha1 *sha2) const{

return false;
}
};

i can add only one object to map (that OK).
But when i always return with true, i can add for example 5 objects but
iterator still iterate only fhtu two of them (like described earlier).
....
Jul 22 '05 #7

P: n/a
Also the way you are using sha1_sprintf_hex looks dubious. If you are
returning a pointer to a dynamically allocated string then you have a memory
leak, if you are returning a pointer to a staticly allocated array then you
have a bug. Perhaps you should post the code for sha1_sprintf_hex.


yes, that's other problem i must resolve.
I edited Compare as you said and everything works fine:)

Thanx
Michal
Jul 22 '05 #8

P: n/a

"vertigo" <ax***@wp.pl> wrote in message
news:ci**********@atlantis.news.tpi.pl...
When i always return with false:
struct Compare{
bool operator()(sha1 *sha1, sha1 *sha2) const{

return false;
}
};

i can add only one object to map (that OK).
But when i always return with true, i can add for example 5 objects but
iterator still iterate only fhtu two of them (like described earlier).
...


If you don't write you comparison functor in the correct way you get
unpredictable results. It must return true if sha1 < sha2 and false
otherwise.

john
Jul 22 '05 #9

P: n/a
vertigo wrote:
When i always return with false:
struct Compare{
bool operator()(sha1 *sha1, sha1 *sha2) const{
return false;
}
};

i can add only one object to map (that OK).
But when i always return with true, i can add for example 5 objects but
iterator still iterate only fhtu two of them (like described earlier).
...


The thing is that keys in the map are compared using only that Compare
function: a == b if neither a < b nor b < a.
So if Compare always return true, then a < b for any a and b; in
particular a is never equal to a, and map can do whatever it wants to
make you feel crazy.

Yevgen
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.