C++ - 如何创建具有 4 个变量和 4 个键的多重映射

发布于 2024-10-19 14:00:17 字数 622 浏览 1 评论 0原文

我想创建一个如下所示的类,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你怎么敢 2024-10-26 14:00:17

在这种情况下,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.

下壹個目標 2024-10-26 14:00:17

这是一个使用 boost.multi_index 的简单示例:

#include <string>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>

struct enumClass
{
  int msdnEnum;
  std::string msdnEnumString; 
  int localEnum;
  std::string localEnumString;
};

namespace bmi = boost::multi_index;

typedef bmi::multi_index_container<
  enumClass,
  bmi::indexed_by<
    bmi::ordered_unique<bmi::member<enumClass, int, &enumClass::msdnEnum> >,
    bmi::ordered_unique<bmi::member<enumClass, std::string, &enumClass::msdnEnumString> >,
    bmi::ordered_unique<bmi::member<enumClass, int, &enumClass::localEnum> >,
    bmi::ordered_unique<bmi::member<enumClass, std::string, &enumClass::localEnumString> >
  >
> enumClassSet;

int main()
{
  enumClassSet enums;
  enums.get<0>().find(/*given_msdnEnum*/);     // index 0 is enumClass::msdnEnum
  enums.get<1>().find(/*given_msdnEnumStr*/);  // index 1 is enumClass::msdnEnumString
  enums.get<2>().find(/*given_localEnum*/);    // index 2 is enumClass::localEnum
  enums.get<3>().find(/*given_localEnumStr*/); // index 3 is enumClass::localEnumString
}

标签类也可用于通过名称而不是序数索引来访问索引,其用法如下所示:

struct byMsdnEnum;
struct byMsdnEnumStr;
struct byLocalEnum;
struct byLocalEnumStr;

typedef bmi::multi_index_container<
  enumClass,
  bmi::indexed_by<
    bmi::ordered_unique<bmi::tag<byMsdnEnum>, bmi::member<enumClass, int, &enumClass::msdnEnum> >,
    bmi::ordered_unique<bmi::tag<byMsdnEnumStr>, bmi::member<enumClass, std::string, &enumClass::msdnEnumString> >,
    bmi::ordered_unique<bmi::tag<byLocalEnum>, bmi::member<enumClass, int, &enumClass::localEnum> >,
    bmi::ordered_unique<bmi::tag<byLocalEnumStr>, bmi::member<enumClass, std::string, &enumClass::localEnumString> >
  >
> enumClassSet;

int main()
{
  enumClassSet enums;
  enums.get<byMsdnEnum>().find(/*given_msdnEnum*/);
  enums.get<byMsdnEnumStr>().find(/*given_msdnEnumStr*/);
  enums.get<byLocalEnum>().find(/*given_localEnum*/);
  enums.get<byLocalEnumStr>().find(/*given_localEnumString*/);
}

两种方法之间的差异纯粹是美学上的,当然还有标签类可以命名为您想要的任何名称,而不是 byMsdnEnum 等。另请注意,可以使用散列索引而不是有序索引,这将使您的索引具有 std::unordered_map 的行为code> 而不是 std::map

Here's a simple example using boost.multi_index:

#include <string>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>

struct enumClass
{
  int msdnEnum;
  std::string msdnEnumString; 
  int localEnum;
  std::string localEnumString;
};

namespace bmi = boost::multi_index;

typedef bmi::multi_index_container<
  enumClass,
  bmi::indexed_by<
    bmi::ordered_unique<bmi::member<enumClass, int, &enumClass::msdnEnum> >,
    bmi::ordered_unique<bmi::member<enumClass, std::string, &enumClass::msdnEnumString> >,
    bmi::ordered_unique<bmi::member<enumClass, int, &enumClass::localEnum> >,
    bmi::ordered_unique<bmi::member<enumClass, std::string, &enumClass::localEnumString> >
  >
> enumClassSet;

int main()
{
  enumClassSet enums;
  enums.get<0>().find(/*given_msdnEnum*/);     // index 0 is enumClass::msdnEnum
  enums.get<1>().find(/*given_msdnEnumStr*/);  // index 1 is enumClass::msdnEnumString
  enums.get<2>().find(/*given_localEnum*/);    // index 2 is enumClass::localEnum
  enums.get<3>().find(/*given_localEnumStr*/); // index 3 is enumClass::localEnumString
}

Tag classes could be used to access the indices by name rather than ordinal index as well, usage of which would look like this:

struct byMsdnEnum;
struct byMsdnEnumStr;
struct byLocalEnum;
struct byLocalEnumStr;

typedef bmi::multi_index_container<
  enumClass,
  bmi::indexed_by<
    bmi::ordered_unique<bmi::tag<byMsdnEnum>, bmi::member<enumClass, int, &enumClass::msdnEnum> >,
    bmi::ordered_unique<bmi::tag<byMsdnEnumStr>, bmi::member<enumClass, std::string, &enumClass::msdnEnumString> >,
    bmi::ordered_unique<bmi::tag<byLocalEnum>, bmi::member<enumClass, int, &enumClass::localEnum> >,
    bmi::ordered_unique<bmi::tag<byLocalEnumStr>, bmi::member<enumClass, std::string, &enumClass::localEnumString> >
  >
> enumClassSet;

int main()
{
  enumClassSet enums;
  enums.get<byMsdnEnum>().find(/*given_msdnEnum*/);
  enums.get<byMsdnEnumStr>().find(/*given_msdnEnumStr*/);
  enums.get<byLocalEnum>().find(/*given_localEnum*/);
  enums.get<byLocalEnumStr>().find(/*given_localEnumString*/);
}

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 of std::unordered_map rather than std::map.

献世佛 2024-10-26 14:00:17

有什么理由不能使用两张地图吗? ( std::map)。如果您需要通过这两个键中的任何一个来查找两个字符串变量,那么您可以通过使用一个映射来解决它,例如将 msdnEnum 与两个字符串配对,然后使用一个可以配对的映射localEnummsdnEnum。通过 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 pair localEnum with msdnEnum. Than any lookup via msdnEnum would be done directly and a lookup via localEnum would first do a translation to msdnEnum and then do a direct lookup.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文