C++ - 如何创建具有 4 个变量和 4 个键的多重映射
我想创建一个如下所示的类,
class enumClass
{
int msdnEnum;
std::string msdnEnumString;
int localEnum;
std::string localEnumString;
}
std::set<enumClass_Objects> enums; // all msdnEnums are unique, same aplies to other three.
enums.find(given_msdnEnum)->FourthVariable;
enums.find(given_localEnum)->FirstVariable;
enums.find(given_msdnEnumStr)->anyVariable;
enums.find(given_localEnumStr)->anyVariable;
我提到了 boost::multiIndex。但我认为这对这个案子没有帮助。有人能说出实现这一目标的方法吗?
编辑 我不太擅长阅读模板类。至于我所关心的,我在 multiIndex 中没有找到任何“查找”方法。我只看到了该示例中的整理内容(第一个基本示例:
I want to create a class like below
class enumClass
{
int msdnEnum;
std::string msdnEnumString;
int localEnum;
std::string localEnumString;
}
std::set<enumClass_Objects> enums; // all msdnEnums are unique, same aplies to other three.
enums.find(given_msdnEnum)->FourthVariable;
enums.find(given_localEnum)->FirstVariable;
enums.find(given_msdnEnumStr)->anyVariable;
enums.find(given_localEnumStr)->anyVariable;
I referred boost::multiIndex. But I don't think that it helps on this case. Can anybody say the way to achieve this?
EDIT
I am not that much good in reading template classes. As for as I am concerning I didn't find any "find" methods in multiIndex. I saw only sorting out things in that example(the first basic example:link). Suggestions&advices are always welcomed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下,boost multiindex 正是您所需要的。为四个键构建四个索引 - 我猜你保证它们都是唯一的,使它们成为唯一索引(我相信只有其中一个可以是 hashed_unique,但我认为你可以使其他三个 order_unique),然后执行您对每个索引的搜索取决于您搜索的内容。
boost multiindex is exactly what you need in this case. Construct four indexes for the four keys - I guess given you guarantee that they will all be unique, make them unique indexes (I believe only one of them can be hashed_unique, but I think you can make the other three ordered_unique), and then do your searches on the each index depending on what you are searching by.
这是一个使用 boost.multi_index 的简单示例:
标签类也可用于通过名称而不是序数索引来访问索引,其用法如下所示:
两种方法之间的差异纯粹是美学上的,当然还有标签类可以命名为您想要的任何名称,而不是
byMsdnEnum
等。另请注意,可以使用散列索引而不是有序索引,这将使您的索引具有std::unordered_map
的行为code> 而不是std::map
。Here's a simple example using boost.multi_index:
Tag classes could be used to access the indices by name rather than ordinal index as well, usage of which would look like this:
The difference between the two approaches is purely aesthetic, and of course the tag classes could be named whatever you want rather than
byMsdnEnum
, etc. Also note that hashed indices could be used rather than ordered indices, which would give your indices the behavior ofstd::unordered_map
rather thanstd::map
.有什么理由不能使用两张地图吗? ( std::map)。如果您需要通过这两个键中的任何一个来查找两个字符串变量,那么您可以通过使用一个映射来解决它,例如将
msdnEnum
与两个字符串配对,然后使用一个可以配对的映射localEnum
与msdnEnum
。通过msdnEnum
进行的任何查找都将直接完成,而通过localEnum
进行的查找将首先转换为msdnEnum
,然后进行直接查找。Is there a reason why you cannot use 2 maps? ( std::map< int, std::string> ). If you need to find both of the string variables by any of those two keys, then you could solve it by having a map that would pair for example
msdnEnum
with both strings and then have a map that would pairlocalEnum
withmsdnEnum
. Than any lookup viamsdnEnum
would be done directly and a lookup vialocalEnum
would first do a translation tomsdnEnum
and then do a direct lookup.