If you dereference them you can compare the actual values of the objects in the two containers. Maybe that's what you want? Don't dereference an iterator equal to yourContainer.end() though.
I thought along those lines... but then a problem I envisioned is that it's possible for the objects to be perfectly equal in value, therefore you'd need to know if they were different actual instances of such objects, and then we're probably back at square one...
Basically, it's for a system for moving items between 2 inventories. So Inventory A might have 200 of Item X and that might be selected, but Inventory B could also have 200 of Item X... so it's very important that it refers to the exact instances involved.
It would be very helpful if you could post a small program that shows what you tried.
I'll try to cut out the relevant bits that hopefully show enough to get the idea across without getting into anything irrelevant...
- I have an Inventory class which represents the inventory of, for example, a player.
This contains a map consisting of a pair representing the item name and its condition as an int, and another int as the value of the map which represents the quantity:
std::map< std::pair< std::string, int >, int > itemStock;
- This stock container can be accessed like so:
const std::map< std::pair< std::string, int >, int >& getStock();
- I have an iterator to remember the currently selected item for the purposes of a user interface:
std::map< std::pair< std::string, int >, int >::const_iterator selInvItem;
- What I tried then as I iterated through an inventory's stock container, was the following:
for( std::map< std::pair< std::string, int >, int >::const_iterator it = stock.begin(); it != stock.end(); it++ )
{
if( selInvItem == it )
{
//Found the currently selected item
}
}
But it crashes with "Expression: map/set iterators incompatible".
Have I missed something?
I stepped through it withe breakpoints and the iterator I was using (selInvItem) didn't seem to be getting initialized for some reason... at least it was showing up as 0... but either way I have it so "it" is being compared with another iterator that shows up like it should when I step through the program and it still gives the same error.
Is comparing iterators that point to different containers just not a thing you do?