C++ STD::MAP 复杂键搜索

发布于 2024-10-28 04:12:38 字数 183 浏览 0 评论 0原文

我对库的访问权限有限,因此尽管使用 boost::multi_index 可以解决我的问题,但我无法使用它。

我当前的地图设置是: 该结构包含大量信息,例如我还需要搜索的 INT。我所希望的是这样的结构,以便我可以按 int 或字符串搜索并返回结构值。我假设我必须写下密钥,但是我来这里是为了寻求其他建议。

有想法吗?

I have limited access to libraries so although using boost::multi_index would solve my issue it is something that I cannot use.

My current map setup is:
The structure contains a fair amount of information in it such as an INT that I will also need to search by. What I was hoping was a structure such as so that I can search by int or string and return the structure values. I am assuming that I am going to have to write the key but, was coming here for other suggestions.

Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

愿与i 2024-11-04 04:12:38

我有点困惑。您似乎是在说您有这样的构造:

(psudocode)

struct Gizmo
{
  Gizmo(int foo, string bar) : foo_(foo), bar_(bar) {};
  int foo_;
  string bar_;
};
Gizmo make_gizmo(int foo, string bar) { return Gizmo(foo,bar); }
std::map<string, Gizmo> my_gizmos;

my_gizmos["aaa"] = make_gizmo(1,"hello");
my_gizmos["bbb"] = make_gizmo(2,"there");

...并且您希望能够通过 foo_ 的值搜索 Gizmo

在这种情况下,您有两个主要选择。

1) 只需自己编写一个自定义函子(再次使用 psudocude):

struct match_foo : public std::unary_function<...>
{
  match_foo(int foo) : foo_(foo) {};
  bool operator()(map<string,Gizmo>::const_iterator it) const
  {
    return it->second.foo_ == foo_;
  } 
private:
  int foo_;
};

map<string,Gizmo>::const_iterator that = find_if(my_gizmos.begin(), my_gizmos.end(), match_foo(2));
};

2) 创建 foo_ 值的索引,映射回主 map< 中的 Gizmo /代码>。这张地图可能看起来像这样
...
a

   map<int,map<string,Gizmo>::const_iterator> foo_index;

...您每次更新主地图my_gizmos时都会维护它。

I'm a little confused. You seem to be saying that you have a construct like this:

(psudocode)

struct Gizmo
{
  Gizmo(int foo, string bar) : foo_(foo), bar_(bar) {};
  int foo_;
  string bar_;
};
Gizmo make_gizmo(int foo, string bar) { return Gizmo(foo,bar); }
std::map<string, Gizmo> my_gizmos;

my_gizmos["aaa"] = make_gizmo(1,"hello");
my_gizmos["bbb"] = make_gizmo(2,"there");

...and you want to be able to search for Gizmos by the value of foo_?

In that case, you have 2 main options.

1) Just write a custom functor yourself (again psudocude):

struct match_foo : public std::unary_function<...>
{
  match_foo(int foo) : foo_(foo) {};
  bool operator()(map<string,Gizmo>::const_iterator it) const
  {
    return it->second.foo_ == foo_;
  } 
private:
  int foo_;
};

map<string,Gizmo>::const_iterator that = find_if(my_gizmos.begin(), my_gizmos.end(), match_foo(2));
};

2) Create an index of the foo_ values, mapping back to the Gizmo in the main map. This map might look something like this
...
a

   map<int,map<string,Gizmo>::const_iterator> foo_index;

...which you would maintain any time you update the main map, my_gizmos.

煮酒 2024-11-04 04:12:38

如果您的搜索实际上是窗口查询(意味着您必须返回 [param0_0, peram0_1]x[param1_0, param1_2]x...)中的值,那么您可以使用范围树结构来提高效率。

If your searches actually are windowing queries(meaning you must return values in [param0_0, peram0_1]x[param1_0, param1_2]x...) or so, then you can use range tree structure for efficiency.

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