• Popular Now

• 13
• 18
• 19
• 27
• 10

Archived

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

STL Maps

This topic is 5110 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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 on other sites
huh? this is my map (the < was edited out)

typedef map < coor, MyImage *, eqstrc > Layer;

Share on other sites
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 on other sites
Have you tried to overload the
operator<
You wont need a comparator then.
You could implement it like

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

Share on other sites
return x + y * MAX_X < op2.x + op2.y * MAX_X;

that is ^^

Share on other sites
quote:
Original post by Anonymous Poster
Have you tried to overload the
operator<
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 on other sites
#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 on other sites
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 on other sites
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 on other sites
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();}