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 9 1459
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
"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
> 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
"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
"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
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).
.... 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
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
by: hokus |
last post by:
I have the following class:
<CODE>
class List
{
public:
class Node
{
|
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...
|
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);
|
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;...
|
by: subramanian100in |
last post by:
consider the following program:
#include <cstdlib>
#include <iostream>
#include <map>
#include <utility>
#include <string>
using namespace std;
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
| |