If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

I'm getting a "passing...discards qualifiers" error on my if statement and not sure why because I'm not changing anything. I know removing const or making test mutable fixes the issue. I've been taught to always make a function const if it doesn't change anything, in which case, have I finally come across an acceptable time to use mutable?

Re: Map Comparison Fails in Function Const

Originally Posted by JaySoyer

Well that's a sneaky little devil. I'm so used to checking for null pointers that way, I neglected to see that there was a find method for maps.

It's even more dangerous than just "it's non const". If that data does not exist, it is default constructed, which means initialization with a non-NULL pointer. In turn, testing said pointer against 0 becomes irrelevant (!)

Thank goodness you declared your IsNull as const, as the compiler saved you from shooting your own foot, and potentially hours worth of debugging. What a nightmare!

----
I'm personally not a huge fan of map's interface. It is convenient, sure, because of the subscript syntax, but does not correctly model the map's inner working.

I wrote myself little "get" and "set" functions for interface, inspired from boost's property map library. Not only is it more efficient, it also leaves little room for surprises. That and Meyers even says you should do it!

Is your question related to IO?
Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Re: Map Comparison Fails in Function Const

Originally Posted by monarch_dodra

I'm personally not a huge fan of map's interface. It is convenient, sure, because of the subscript syntax, but does not correctly model the map's inner working.

I wrote myself little "get" and "set" functions for interface, inspired from boost's property map library. Not only is it more efficient, it also leaves little room for surprises. That and Meyers even says you should do it!

So you mean the subscripting takes away from the fact its a Red Black tree? I didn't think you could get more efficient then O(Log N) access with that type of tree.

I always here so many references for using Boost. One of these days I'll get around to it.

Re: Map Comparison Fails in Function Const

So you mean the subscripting takes away from the fact its a Red Black tree? I didn't think you could get more efficient then O(Log N) access with that type of tree.

Asymtotically, operator[] has the same efficiency as using lower_bound followed by insert if necessary. But using lower_bound followed by insert is faster by a constant factor.

Note: I suggested using lower_bound rather than find. If you are just querying whether a given item exists, then find is okay; but if you wish to insert the item if it does *not* exist, then lower_bound is slightly better because the iterator returned from it (even if it doesn't point to the item you want) can be used as a hint to insert() to speed up that function.

The reason why this is faster than operator[] is because in the expression m[i] = A where i does not exist in map m, a key will first be default-constructed and then copied over; by contrast, with lower_bound followed by insert, only a copy construction is necessary. The difference is minor but nonzero.

I always here so many references for using Boost. One of these days I'll get around to it.

Boost is a collection of useful things. Some parts of it are more intimidating than others. You should take a look to see what's available, but don't feel compelled to start using everything in there immediately.