map & iterator

V

vertigo

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_iterator 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
 
Y

Yevgen Muntyan

vertigo said:
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_iterator 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
 
J

John Harrison

vertigo said:
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_iterator 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
 
V

vertigo

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
 
J

John Harrison

vertigo said:
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
 
J

John Harrison

John Harrison said:
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
 
V

vertigo

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).
....
 
V

vertigo

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
 
J

John Harrison

vertigo said:
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
 
Y

Yevgen Muntyan

vertigo said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top