473,378 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

map & iterator

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
9 1457
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

"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
> 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

"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

"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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
7
by: Patrick | last post by:
In class *ClassA* below I have an STL Vector *vec* as a member variable of the class. Do I have to create a destructer, and somehow deallocate the memory from *vec*, or is this handled...
6
by: NKOBAYE027 | last post by:
FIRST POST Hi All: I'm trying to write a simple specialization before moving on to something a bit more complex - always a good idea in my case, at least. :o) I'm trying to adapt the example...
7
by: lmanchur. | last post by:
Hi, I am implementing a version of the C++ STL map class for a school assignment and have a question... what is the difference between const_iterator and just iterator??... If I already have an...
9
by: Baloff | last post by:
Hello group Is there a library which can help in doing the task below or do I need to write something up? I need to get the index of the relative “local” maximas and minimas in a sequence or...
4
by: hokus | last post by:
I have the following class: <CODE> class List { public: class Node {
1
by: pedagani | last post by:
Hello Comp.lang.c++ members, Consider the " begin " routine for a class with an interator. Iterator begin() { return( Iterator( _class_element ) ); } where the constructor for the iterator is...
9
by: miaohua1982 | last post by:
the program is as follows: #include <vector> using namespace std; class A{}; int main() { A* const &p = NULL; vector<A*B(3,NULL); //there is a compile error B.push_back(NULL);
1
by: Saile | last post by:
I want to give an array the values from the specific multimap's key's values. multimap<string,int> mymultimap; multimap<string,int>::iterator it;...
0
by: subramanian100in | last post by:
consider the following program: #include <cstdlib> #include <iostream> #include <map> #include <utility> #include <string> using namespace std;
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.