STL: set with map usage

Hi,
I have a following problem:
I want to use set that contains map (s).
Here are important parts of my code:
1
2
3
4
5
6
7
8
struct position{
	int t[2];
};
//...
//somewhere in main:
map< position, bool> p;
set < map <position, bool> > container;
container.insert(p);

and I'm getting this error:

>main.obj : error LNK2001: unresolved external symbol "bool __cdecl operator<(class std::map<struct position,bool,struct std::less<struct position>,class std::allocator<struct std::pair<struct position const ,bool> > > const &,class std::map<struct position,bool,struct std::less<struct position>,class std::allocator<struct std::pair<struct position const ,bool> > > const &)" (??M@YA_NABV?$map@Upozycja@@_NU?$less@Uposition@@@std@@V?$allocator@U?$pair@$$CBUposition@@_N@std@@@3@@std@@0@Z)



What do I wrong?
Last edited on
I want to use set that contains map (s).

Why do you want to do this?
std::map wants to know how to sort your positions in order, but it can't figure out how. You need to let it know how:
1
2
3
4
5
6
7
8
struct position{
	int t[2];

	bool operator<(const position &other) const
	{
		return t[0] < other.t[0] || t[1] < other.t[1];
	}
};
With this change, your code compiles for me. I would still heed cire's post, though.
Last edited on
@cire:
complexity is very important for me here, and set has quick access with its sorted items;
@L B
I have this operator< and it sorts my positions correctly (edited)

1
2
3
4
5
6
7
bool operator < ( const position &a, const position &b ){
	if( a.t[0] < b.t[0] )
		return true;
	if( a.t[0] == b.t[0] && a.t[1] < b.t[1] )
		return true;
	return false;
};
Last edited on
@pcej so is the problem solved? Your response is not very clear.
not really, it doesnt work and there is no progress
Your version of the operator is equivalent to my version.

Are you sure it still does not work? When I compile your code including your version of the operator, it compiles and runs successfully.
1
2
3
4
5
6
7
int countNext( map<position, bool> p, test t){
	set < map <position, bool> > container;

	container.insert(  p);

	return 0;
};


This is the function that I'm starting to write down. If I comment line 4 it compiles.

Do you have any other ideas?

PS.
I've tried with all stl containers and only vector compiles, but .find() is necessery for me
Last edited on
Does pozycja have an overloaded operator<?
Edited
My question remains, but for position.
Last edited on
do I need any overloaded operator<? why ? I was told set is able to compare maps(or other stl containers) just like that without operator. ;/

bool operator < ( const map <pozycja, bool> &a, const map <pozycja, bool> &b );

That's the only one operator that I have. Should I write an operator< for set as well ?
edited: solved
Last edited on
The error is not related to the set. The error is related to the map not being able to compare your key values. Map keys MUST be comparable via operator<:
http://www.cplusplus.com/reference/map/map/
(see description of "Compare")
Topic archived. No new replies allowed.