Re: Map losing elements!?
This is a MIME GnuPG-signed message. If you see this text, it means that
your E-mail or Usenet software does not support MIME signed messages.
The Internet standard for MIME PGP messages, RFC 2015, was published in 1996.
To open this message correctly you will need to install E-mail or Usenet
software that supports modern Internet standards.
--=_mimegpg-commodore.email-scan.com-26790-1222910549-0001
Content-Type: text/plain; format=flowed; charset="US-ASCII"
Content-Disposition: inline
Content-Transfer-Encoding: 7bit
Johannes Bauer writes:
Hello group,
I've been trying around with a simple std::map<mytype, unsigned> for an
hour now and can't find the bug. It is my belief that I am missing
something incredibly obvious... please help me see it.
Scenario: I created the map and inserted some elements. Afterwards I try
to iterate over them:
std::map<mytype, unsigned int> values;
/* insert some values, say 5 */
/* this will then report 5 */
std::cerr << "cnt = " << values.size() << std::endl;
for (std::map<mytype, unsigned int>::iterator j = values.begin(); j !=
values.end(); j++) {
std::cerr << j->second << " -> ";
j->first.Dump();
std::cerr << std::endl;
}
However in the "for" loop, always only 2 items show up. I figured
something was wrong with my operator< - essentialy "mytype" is just a
container around a LENGTH byte unsigned char[] array:
bool operator<(const mytype &Other) const {
for (unsigned int i = 0; i < LENGTH; i++) {
if (Other.Data[i] < Data[i]) return true;
}
return false;
}
Can anyone explain this behaviour?
Although your operator< does look wrong, this wouldn't really explain your
claimed problem. If there are 5 elements in the map, then there are 5
elements in the plan. Unless, the iterator's operator++ invokes the
comparison function. I don't recall if it does. Although I don't see why it
would, it may.
Aside from the semantical meanings of your comparison operator, and without
knowing any other details of your custom classes (which may be relevant), it
does look like you need
if (Other.Data[i] > Data[i]) return false;
appended to the inner body if your for loop, after the existing if
statement.
A simple paper-and-pencil excersize should show you why this is the case.
--=_mimegpg-commodore.email-scan.com-26790-1222910549-0001
Content-Type: application/pgp-signature
Content-Transfer-Encoding: 7bit
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
iEYEABECAAYFAkjkIlUACgkQx9p3GYHlUOKV2wCfd1v5XAc20BLPLayRecZ6CizV
sboAnimERUF9dJTZY9rZtz43ttvISflL
=wbnX
-----END PGP SIGNATURE-----
--=_mimegpg-commodore.email-scan.com-26790-1222910549-0001--