Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL map problem

Reply
Thread Tools

STL map problem

 
 
arkandor
Guest
Posts: n/a
 
      06-24-2008
Hello,

I have following code. I wrote it in Borland C++ Builder 5.0.

#include <map>
#include <iostream>

using namespace std;

struct TVec
{
double x, y, z;
bool operator !=(const TVec& v2) const
{ return ((fabs(x-v2.x)>1e-12) || (fabs(y-v2.y)>1e-12) ||
(fabs(z-v2.z)>1e-12));}
};

typedef map<TVec, int, not_equal_to<TVec> > TMap;

int main()
{
TMap m;
TMap::iterator iter;

TVec v;

v.x=3.1; v.y=2.3; v.z=4.1;
m[v] = 32;
v.x=-3.1; v.y=2.3; v.z=4.1;
m[v]=45;
v.x=-3.25; v.y=2.3; v.z=4.1;
m[v] = 55;

cout<<"size = "<<m.size()<<endl;

v.x=3.1; v.y=2.3; v.z=4.1;
iter = m.find(v);
if (iter != m.end())
cout<<iter->second<<endl;

v.x=-3.1; v.y=2.3; v.z=4.1;
iter = m.find(v);
if (iter != m.end())
cout<<iter->second<<endl;

v.x=-3.25; v.y=2.3; v.z=4.1;
iter = m.find(v);
if (iter != m.end())
cout<<iter->second<<endl;

system("pause");
return 0;
}

After compilation we have on the screen :
size=3
32
45

However third element exsists in the map it isn't found by m.find(TVec)
(!!!)
I would be grateful for any suggestions.
Best regards, arkandor


 
Reply With Quote
 
 
 
 
huili80@gmail.com
Guest
Posts: n/a
 
      06-24-2008
On Jun 24, 7:01*am, "arkandor" <arkando...@poczta.onet.pl> wrote:
> Hello,
>
> I have following code. I wrote it in Borland C++ Builder 5.0.
>
> #include <map>
> #include <iostream>
>
> using namespace std;
>
> struct TVec
> *{
> * *double x, y, z;
> * *bool operator !=(const TVec& v2) const
> * *{ return ((fabs(x-v2.x)>1e-12) || (fabs(y-v2.y)>1e-12) ||
> (fabs(z-v2.z)>1e-12));}
> *};
>
> typedef map<TVec, int, not_equal_to<TVec> > TMap;
>
> int main()
> {
> *TMap m;
> *TMap::iterator iter;
>
> *TVec v;
>
> *v.x=3.1; v.y=2.3; v.z=4.1;
> *m[v] = 32;
> *v.x=-3.1; v.y=2.3; v.z=4.1;
> *m[v]=45;
> *v.x=-3.25; v.y=2.3; v.z=4.1;
> *m[v] = 55;
>
> *cout<<"size = "<<m.size()<<endl;
>
> *v.x=3.1; v.y=2.3; v.z=4.1;
> *iter = m.find(v);
> *if (iter != m.end())
> * cout<<iter->second<<endl;
>
> *v.x=-3.1; v.y=2.3; v.z=4.1;
> *iter = m.find(v);
> *if (iter != m.end())
> * cout<<iter->second<<endl;
>
> *v.x=-3.25; v.y=2.3; v.z=4.1;
> *iter = m.find(v);
> *if (iter != m.end())
> * cout<<iter->second<<endl;
>
> *system("pause");
> *return 0;
>
> }
>
> After compilation we have on the screen :
> size=3
> 32
> 45
>
> However third element exsists in the map it isn't found by m.find(TVec)
> (!!!)
> I would be grateful for any suggestions.
> Best regards, arkandor


So you use the not_equal_to<TVec> as the less_than compare for the
std::map, right?
which means whenever you have v1<v2, you also have v2<v1, where "<" is
what your map uses as a less_than compare ...
It doesn't suprise me all that you got non-sense result, because your
less_than compare doesn't make much sense.
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      06-24-2008
On Jun 24, 1:01 pm, "arkandor" <arkando...@poczta.onet.pl> wrote:

> I have following code. I wrote it in Borland C++ Builder 5.0.


> #include <map>
> #include <iostream>


> using namespace std;


> struct TVec
> {
> double x, y, z;
> bool operator !=(const TVec& v2) const
> { return ((fabs(x-v2.x)>1e-12) || (fabs(y-v2.y)>1e-12) ||
> (fabs(z-v2.z)>1e-12));}
> };


> typedef map<TVec, int, not_equal_to<TVec> > TMap;


std::map requires an ordering relationship, not an equivalence
relationship. (Not that you've implemented either; your
definition of != doesn't establish an equivalence relationship
either.)

From that point on, behavior is undefined.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
arkandor
Guest
Posts: n/a
 
      06-26-2008
So you use the not_equal_to<TVec> as the less_than compare for the
std::map, right?
which means whenever you have v1<v2, you also have v2<v1, where "<" is
what your map uses as a less_than compare ...
It doesn't suprise me all that you got non-sense result, because your
less_than compare doesn't make much sense.

I've already solved this "problem". When I properly defined < operator
everything works fine. Thanks anyway.

arkandor


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
STL map or hash map using struct as data and find it kl C++ 7 01-01-2008 11:05 AM
a stl map which use stl pair as the key Allerdyce.John@gmail.com C++ 2 02-22-2006 07:25 AM
STL: Map of maps possible, but no multi-map of maps? Workarounds? Marcus C++ 2 12-09-2005 06:34 AM
Problem with distance and STL map tron.thomas@verizon.net C++ 2 09-13-2005 10:35 PM
STL map and pthread performance problem on Linux/GCC nan.li.g@gmail.com C++ 9 08-18-2005 03:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57