Question

I have the following code:

wxString getColorName(const wxColour& color)
{
    typedef ColorComboBox::ColorMap::right_const_iterator ConstColorIterator;
    ColorComboBox::ColorMap colorMap = ColorComboBox::getDefaultChoices();
    ConstColorIterator it = colorMap.right.find(color);
    return it != colorMap.right.end() ? it->second :
            ColorComboBox::CUSTOM_COLOR;
}

where ColorMap is defined

typedef boost::bimaps::bimap \
            <wxString, boost::bimaps::vector_of<wxColour> > \
            ColorMap;

and I keep getting a long template error that basically says the find function does not exist. However

ColorMap::left_const_iterator it = choices_.left.find(GetValue());

compiles fine.
I have a hunch the find function is only defined in certain collection types of bimap. I cannot use a set_of wxColours because wxColour is not comparable. (What would that even mean?) I also tried changing the collection type to list_of, but that didn't work either. My whole point in using bimap was so that I could find values going either way. Am I using the wrong container? Is there another collection type I can use for wxColour that will allow me to use the find function?

EDIT: I ended up creating my own container class.

Was it helpful?

Solution

A Bimap allows you to define the mapping type of each side. If your application needs to perform quick searches, use map/multimap or unordered_map/unordered_multimap based mappings. Please read the documentation and remember that each map view is modeled after the equivalent STL containers, so they share the same constraints and interface:

  • set_of ( ordered, unique ) --> std::map
  • multiset_of ( ordered ) --> std::multimap
  • unordered_set_of ( hashed, unique ) --> std::unordered_map
  • unordered_multiset_of ( hashed ) --> std::unordered_multimap
  • list_of ( sequenced ) --> list_map ( std::list<pair> )
  • vector_of ( random access ) --> vector_map ( std::vector<pair> )
  • unconstrained_set_of --> not mapped

I do not understand why you choose a vector_of<wxColour>, maybe it was just because set_of<wxColour> (the default) didn't compiled... in this case, as you stated, you need to define your own comparison operator to tell the bimap how to order these items. The vector-mapping and list-mapping are there to allow you to create bimaps that preserve the insertion order of your relations.

In your case, what you want is a unordered_set_of. You will need to define your own hash functor for wxColour. You can use Boost.Hash to implement it.

Best regards

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top