存储可通过键或序数 c++ 访问的数据的简单有效的方法

发布于 2024-11-06 01:49:21 字数 278 浏览 0 评论 0原文

我需要创建一个可以通过字符串键或序号访问元素的数据结构。

该类当前使用一个节点数组,其中包含字符串键和指向任何元素的指针。这允许 O(n) 循环,或者 O(1) 按序数获取元素,但是我发现通过键查找元素的唯一方法是执行 O(n) 循环并比较键,直到找到什么我想要的是,当有 1000 多个元素时,速度很慢。有没有办法使用键来引用指针,或者我运气不好?

编辑:按序数并不像 O(n) 循环那么重要。这将用作基础结构,该结构将被继承以其他方式使用,例如,如果它是可绘制对象的结构,我希望能够在单个循环中绘制所有对象

I need to create a data structure that can access elements by a string key, or by their ordinal.

the class currently uses an array of nodes that contain the string key and a pointer to whatever element. This allows for O(n) looping through, or O(1) getting an element by ordinal, however the only way I've found to find an element by key is doing an O(n) loop and comparing keys until I find what I want, which is SLOW when there are 1000+ elements. is there a way to use the key to reference the pointer, or am I out of luck?

EDIT: the by ordinal is not so much important as the O(n) looping. This is going to be used as a base structure that will be inherited for use in other ways, for instance, if it was a structure of draw able objects, i'd want to be able to draw all of them in a single loop

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

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

发布评论

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

评论(4

叫思念不要吵 2024-11-13 01:49:21

您可以使用 std::map 来获得 O(log n) 搜索速度。查看此分支 了解更多详情。在这个分支中,我们讨论了您的具体情况(通过 string 或/和 ordinal 键快速检索值)。

小例子(使用序数键,你可以用字符串做类似的事情):

#include <map>
#include <string>

using std::map;
using std::string;

struct dummy {
 unsigned ordinal_key;
 string dummy_body;
};

int main() 
{
 map<unsigned, dummy> lookup_map;
 dummy d1;
 d1.ordinal_key = 10;
 lookup_map[d1.ordinal_key] = d1;
 // ...
 unsigned some_key = 20;
 //determing if element with desired key is presented in map
 if (lookup_map.find(some_key) != lookup_map.end())
  //do stuff
}

You can use std::map for O(log n) searching speed. View this branch for more details. In this branch exactly your situation (fast retrieving values by string or/and ordinal key) is discussed.

Small example (ordinal keys are used, you can do similiar things with strings):

#include <map>
#include <string>

using std::map;
using std::string;

struct dummy {
 unsigned ordinal_key;
 string dummy_body;
};

int main() 
{
 map<unsigned, dummy> lookup_map;
 dummy d1;
 d1.ordinal_key = 10;
 lookup_map[d1.ordinal_key] = d1;
 // ...
 unsigned some_key = 20;
 //determing if element with desired key is presented in map
 if (lookup_map.find(some_key) != lookup_map.end())
  //do stuff
}
数理化全能战士 2024-11-13 01:49:21

如果您很少修改数组,则可以保持其排序并对其使用 binary_searchO(logn) 时间内通过键查找元素(从技术上讲,O(klogn),因为您使用的是字符串 [其中 k 是键字符串的平均长度] ).
当然,这(就像使用 map 或 unordered_map 一样)会弄乱您的序数检索,因为元素将以排序顺序而不是插入顺序存储。

If you seldom modify your array you can just keep it sorted and use binary_search on it to find the element by key in O(logn) time (technically O(klogn) since you're using strings [where k is the average length of a key string]).
Of course this (just like using a map or unordered_map) will mess up your ordinal retrieval since the elements are going to be stored in sorted order not insertion order.

无名指的心愿 2024-11-13 01:49:21

使用向量和映射:

std::vector<your_struct> elements;
std::map<std::string, int> index;

映射允许您在 O(lg n) 时间内检索键的索引,而向量允许通过索引访问 O(1) 元素。

Use vector and map:

std::vector<your_struct> elements;
std::map<std::string, int> index;

Map allows you to retrieve the key's index in O(lg n) time, whereas the vector allows O(1) element access by index.

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