升压bimap的find()问题 [英] find() problems with boost bimap

查看:84
本文介绍了升压bimap的find()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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;
}

定义ColorMap的地方

where ColorMap is defined

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

,并且我一直收到一个较长的模板错误,该错误基本上表明find函数不存在.但是

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());

编译正常.
我有一种预感,仅在某些特定的bimap集合类型中定义了find函数.我无法使用set_of wxColours,因为wxColour不具有可比性. (那甚至意味着什么?)我还尝试将集合类型更改为list_of,但这也不起作用.使用bimap的全部目的是使我可以找到任一种方式的值.我使用了错误的容器吗?我可以为wxColour使用另一种收集类型,使我可以使用find函数吗?

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?

我最终创建了自己的容器类.

I ended up creating my own container class.

推荐答案

Bimap允许您定义

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(有序,唯一)-> std::map
  • multiset_of(已排序)-> std::multimap
  • unordered_set_of(哈希,唯一)-> std::unordered_map
  • unordered_multiset_of(散列)-> std::unordered_multimap
  • list_of(已排序)-> list_map(std::list<pair>)
  • vector_of(随机访问)-> vector_map(std::vector<pair>)
  • unconstrained_set_of->未映射
  • 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

我不明白您为什么选择vector_of<wxColour>,也许只是因为set_of<wxColour>(默认值)未编译...在这种情况下,正如您所说的,您需要定义自己的比较运算符告诉bimap如何订购这些物品.通过矢量映射和列表映射,可以创建保留映射关系插入顺序的双图.

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.

在您的情况下,您想要的是 unordered_set_of .您将需要为wxColour定义自己的哈希函子.您可以使用Boost.Hash来实现它.

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.

最诚挚的问候

这篇关于升压bimap的find()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆