473,664 Members | 2,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

map & iterator

Hello
I have strange problem, i have map<myclass1*,m yclass2*,Compar e>.
Code:
std::map<myclas s1*,myclass2*,C ompare> m=mymap;
std::map<myclas s1*,myclass2*,C ompare>::const_ iterator ci;

printf("Element s: %d\n",m.size()) ;
for(ci=m.begin( );ci!=m.end();c i++){
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 1480
vertigo wrote:
Hello
I have strange problem, i have map<myclass1*,m yclass2*,Compar e>.
Code:
std::map<myclas s1*,myclass2*,C ompare> m=mymap;
std::map<myclas s1*,myclass2*,C ompare>::const_ iterator ci;

printf("Element s: %d\n",m.size()) ;
for(ci=m.begin( );ci!=m.end();c i++){
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*,m yclass2*,Compar e>.
Code:
std::map<myclas s1*,myclass2*,C ompare> m=mymap;
std::map<myclas s1*,myclass2*,C ompare>::const_ iterator ci;

printf("Element s: %d\n",m.size()) ;
for(ci=m.begin( );ci!=m.end();c i++){
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_he x(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_he x(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,4 0) < 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_he x(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,4 0) < 0;
}
};


Also the way you are using sha1_sprintf_he x 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_he x.

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_he x 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_he x.


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.new s.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
6817
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 elements, attributes, ".", and "..", plus also the "" predicate format is supported - however, only one predicate per path step is supported, and expr must be a relative path. 2. Poor performance
7
1398
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 automatically? Also I want to allow a client of *ClassA* to be able to view the elements of the vector. Returning a reference to *vec* would be bad OO programming as i would be returning a reference to a private member of the class. I was thinking of...
6
1768
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 from Stroustrup, 3rd ed., The C++ Programming Language p. 344 I'm using MSDev 6.0 in case that's an issue. Here's the source...
7
2719
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 iterator implemented, can I just copy that code and change return types to const and would that be sufficient?? I am lost 'cuz I don't know this definition! Thanks in advance! :)
9
6973
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 real numbers. That is when the derivative is zero and there is a change in direction. Thanks
4
423
by: hokus | last post by:
I have the following class: <CODE> class List { public: class Node {
1
1777
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 defined as Iterator::Iterator( Node* example) :_iter_element ( example ) {}; what confuses me is the way the constructor is used ( Iterator( _class_element ) ).
9
1850
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
2711
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; pair<multimap<string,int>::iterator,multimap<string,int>::iterator> ret; mymultimap.insert (pair<string,int>("test",10)); mymultimap.insert (pair<string,int>("test",20)); mymultimap.insert (pair<string,int>("ab",100)); mymultimap.insert (pair<string,int>("ab",200));
0
1390
by: subramanian100in | last post by:
consider the following program: #include <cstdlib> #include <iostream> #include <map> #include <utility> #include <string> using namespace std;
0
8861
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7375
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4185
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1759
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.