Archived

This topic is now archived and is closed to further replies.

Udi

STL Maps

Recommended Posts

Udi    122
Hi All, This might be a really silly/basic question, but I can''t seem to get this right. I''m trying to create a function that puts a coordinate structe (x,y) into a map, and returns a value. This is easy to do. When I put a coordinate that''s already in the map, it overwrites the previous one. This too is good. However, I am not able to create a function that checks whether the struct is already in the map. I don''t get this!!! This is my struct: struct coor { coor(int a, int b) { x = a; y = b; } int x; int y; }; and this is my map: typedef map Layer; No matter which function I put into eqstrc, this doesn''t work. Any help will be greatly appreciated :-( Udi

Share this post


Link to post
Share on other sites
Sneftel    1788
If you want code to show up correctly, encase it in [ source ] and [ /source ] tags (remove the spaces).

What are you using for your eqstrc function?


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Have you tried to overload the
operator< 
for your coord struct?
You wont need a comparator then.
You could implement it like

return x + y * MAX_Y < op2.x + op2.y * MAX_Y; 

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster

return x + y * MAX_X < op2.x + op2.y * MAX_X;

that is ^^

Share this post


Link to post
Share on other sites
Udi home    122
quote:
Original post by Anonymous Poster
Have you tried to overload the
operator<  
for your coord struct?
You wont need a comparator then.
You could implement it like

return x + y * MAX_Y < op2.x + op2.y * MAX_Y;  




I actually tried, but no matter what I did I kept getting some message about number of parameters is too high? (compilation error). I''d attach it but I''m at home..
Do you happen to have, perhaps, a sample how to do something like that? No matter what I try it won''t compile..

Many many thanks,
Udi

Share this post


Link to post
Share on other sites
T1mb0    134

#include <map>
#include <iostream>

struct coor
{
coor(int a, int b)
{
x = a;
y = b;
}

int x;
int y;

bool operator<( const coor op2 ) const
{
return (x + 1000*y) < (op2.x + 1000*op2.y);
}

};

int main(int argc, char* argv[])
{
std::map<coor,int> layer;

layer[ coor(5,3) ] = 234;
std::cout << layer[ coor(5,2) ] << std::endl;
std::cout << layer[ coor(5,3) ] << std::endl;
std::cout << layer[ coor(4,3) ] << std::endl;

std::cin.get();
}

Share this post


Link to post
Share on other sites
Aprosenf    372
The problem is that a map object automatically discards duplicate objects. If you try to insert i and j and neither (i < j) nor (j < i) evaluates to true (that is implicitly i == j), then then the later object inserted is thrown away. To counter this, the STL has graciously provided you with the multimap object, conveniently located in the header <map> nearest you. It is almost identical to a map, with the exception that duplicates are retained.

Share this post


Link to post
Share on other sites
Udi    122
quote:
Original post by Aprosenf
The problem is that a map object automatically discards duplicate objects. If you try to insert i and j and neither (i < j) nor (j < i) evaluates to true (that is implicitly i == j), then then the later object inserted is thrown away. To counter this, the STL has graciously provided you with the multimap object, conveniently located in the header <map> nearest you. It is almost identical to a map, with the exception that duplicates are retained.


That''s the thing exactly: I don''t want to retain multiple items. I just want to check whether an element is in the map already or not, and from some reason it doesn''t work (i.e., sometimes the results are right, and sometimes they are wrong). I''ll try overloading the < operator and see if it makes a difference. Either way, many many thanks!!!!!

Share this post


Link to post
Share on other sites
Udi    122
Hey - it is working!!!! Thanks so much. I wonder why, it uses the < operator to distinguish between element?


quote:
Original post by T1mb0

#include <map>
#include <iostream>

struct coor
{
coor(int a, int b)
{
x = a;
y = b;
}

int x;
int y;

bool operator<( const coor op2 ) const
{
return (x + 1000*y) < (op2.x + 1000*op2.y);
}

};

int main(int argc, char* argv[])
{
std::map<coor,int> layer;

layer[ coor(5,3) ] = 234;
std::cout << layer[ coor(5,2) ] << std::endl;
std::cout << layer[ coor(5,3) ] << std::endl;
std::cout << layer[ coor(4,3) ] << std::endl;

std::cin.get();
}



Share this post


Link to post
Share on other sites
petewood    819
quote:
Original post by Udi
Hey - it is working!!!! Thanks so much. I wonder why, it uses the < operator to distinguish between element?



It doesn't have to use < but that is the default. If you wanted to use some other function to provide the StrictWeakOrdering that is required you can supply one when you declare your map.

eg

typedef std::map<int, double, std::greater<double> > MapWhichIsSortedUsingGreaterThan;

MapWhichIsSortedUsingGreaterThan myMap;

Actually though the std::greater function will use < anyway! so this example doesn't really show you much, other than you can put your own function or function object into the map to be used for sorting.

Some of the concepts used by the STL are LessThanComparable and EqualityComparable. These are two properties that are necessary for objects to be used in the containers. There isn't a GreaterThanComparable and NonEqualityComparable concept because they can be made out of the others eg !LessThan and !Equal

http://sgi.com/tech/stl/Map.html
http://sgi.com/tech/stl/StrictWeakOrdering.html

[edited by - petewood on March 23, 2004 7:18:55 AM]

Share this post


Link to post
Share on other sites