find() problems with boost bimap

血红的双手。 提交于 2019-12-12 22:20:03

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/9140559/find-problems-with-boost-bimap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!