I'm new to STL. The thing stumping me about using a map to store arbitrary objects:
std::map<MyClassObj, MyDataObject> MyMap;
is how I find objects. How would MyMap.find (MyClassObjInstance) work for instance? Do I need to implement my own iterator and provide some standard functions which would include some equivalence function? Any examples would be appreciated.
Is there another method to store an associated list of arbitrary objects using standard libraries? I'm already using stl to maintain platform portability, and would prefer not to add another library dependency like BOOST.
std::map
has a third template argument, after key and value, to denote what function is going to be used to compare keys. By default, it is std::less
, which in it's turn uses operator<
. So if your class has an operator<, it's ok, else you can provide a comparator of your own.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With