关于使用哪种数据结构来实现快速搜索的建议 C++
我目前正在思考可能用于当前项目的数据结构。我不需要删除项目,因为我正在加载数据库,使用它,然后退出程序。唯一的限制涉及搜索时间。(第二次记忆,但主要是时间)。
概述我打算做什么。 我正在解析文件并提取用于创建各种对象的信息。 读取文件并创建对象后,我有一组多个对象,它们以字符串形式引用其他对象。
这里的目标是找到哪个网络从一个域到另一个域
例如:文本输入文件:
module Blabla
netTomodule Foo
domain 1
..../*Other parameters of the module*/
end module
module Foo
netTomodule Blabla
netTomodule Foo2
domain 2
..../*Other parameters of the module*/
end module
module Foo2
netTomodule Foo
domain 2
..../*Other parameters of the module*/
end module
读完本文后,我得到 3 个模块对象 Foo Foo2 和 Blabla ,它们的属性如下:
class Module{
private :
string name;
int domain;
netlist * mynetlist;
...
}
My意见和我想获得建议的事情:
考虑到这一点后,我认为我最好的办法是:
- 在读取文件并提取信息时,我应该创建一个 Module.txt 的链接列表。
- 然后,根据我读取的模块数量,我创建一个两倍大小的数组。
- 对于每个模块,我使用哈希函数来哈希模块名称,并将指向该模块的指针放在数组中的给定索引处
- 现在,当我想要查找模块时,我只需计算哈希值并获取指针在给定的索引处(如果由于之前在制作数组时发生冲突而不是好的模块,则增加)
这基本上是哈希表的实现,或者至少是我从我的类中知道的哈希表的实现。
我的问题是这是一个好主意吗?有没有我可以使用的哈希表库来做到这一点?(我听说过并寻找 unordered_map 和 map,但我不知道它是否非常适合我的需求)
这是一个很大的文本,所以我希望它是足够详细,所以如果您有勇气阅读所有内容,谢谢您!
I am currently in the thinking process of the data structure I might use for a current project. I don't need to delete items as i am loading a database,using it, and then exiting the program. The only constraint concerns search time.(memory in a second time but mainly time).
Overview on what I intend to do.
I am parsing files and extracting informations that I use to create various object.
After reading the files and creating the objects,i have a set of multiple objects that have references to other one as string.
The goal here is to find which net goes from one domain to another
E.g : text input file :
module Blabla
netTomodule Foo
domain 1
..../*Other parameters of the module*/
end module
module Foo
netTomodule Blabla
netTomodule Foo2
domain 2
..../*Other parameters of the module*/
end module
module Foo2
netTomodule Foo
domain 2
..../*Other parameters of the module*/
end module
After reading this I get 3 module objects Foo Foo2 and Blabla and their attributes are as follow :
class Module{
private :
string name;
int domain;
netlist * mynetlist;
...
}
My opinion and the thing i want to get advice on :
After thinking about this, i think that my best shot is to :
- When reading the file and extracting info, i should create a linked list of Module.
- Then with the number of Module i have read, i create an array that is double the size of that.
- For each module, I use a hash function to hash the module name and put a pointer to this module at the given index in the array
- Now when i will want to find a module, i just have to compute the hash value and get the pointer at the given index(or increment if its not the good module because of a collision previously in the making of the array)
This is basically an implematation of a hashtable or atleast what i know of a hashtable from my clasess.
My question is Is this a good thought ? Is there a hashtable library i can use that does that ? (i have heard and look for unordered_map and map but i don't know if it fits my needs very well)
This is a huge text so i hope it is detailed enough, so thank you if you have the courage to read everything !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需使用标准库附带的或来自 boost 的任何哈希表即可。大多数将具有
unordered_map
(由 TR1 指定并建议用于 C++0x),就像 boost 一样,但有些将具有std::hash_map
或stdext ::hash_map
不同的实现略有不同,例如原始的 SGI 与 Microsoft。不需要构建列表,直接将对象放入哈希表即可;它允许顺序迭代,尽管它将采用某种固定的随机顺序。
Just use any hash table that comes with your standard library or from boost. Most will have the
unordered_map
(as specified by TR1 and proposed for C++0x) as does boost, but some will have astd::hash_map
orstdext::hash_map
with various implementation being slightly different, e.g. the original SGI vs. Microsoft.You don't need to build a list, just put the objects directly in the hash table; it allows sequential iteration, though it will be in some fixed random order.
您可以维护一个哈希表(字符串 => 指向模块类型对象的指针)而不是链接列表。
再次在类 Module 中,再次维护一个 hashmap 或 string => 的映射指针
you can maintain a hashtable (string => pointer to object of type Module) instead of a link list.
Again inside the class Module, again maintain a hashmap or a map of string => pointer
如果您也对间接关系感兴趣(
Foo2
->Foo
->BlaBla
),那么您基本上就有了一个图表。在这种情况下,您可以使用 Boost.Graph 。If you are interested in indirect relationships as well (
Foo2
->Foo
->BlaBla
) you essentially have a graph. In that case you could use Boost.Graph.