在 c++ 上存储大量字符串的最佳方法是什么?允许快速迭代
去年,我创建了一个 WxWidgets GUI 来将 ps2 游戏安装到 ps2 APA 硬盘中。
该程序的功能之一在于在安装游戏时将原始游戏标题分配给游戏。
为了实现此目的,我需要游戏标题及其区域代码(遵循以下命名:ABCD_123.45
)
数据库当前在 CSV 中配置有 15701
条目。
该程序当前将此数据库保存为 std::string
数组(1 个索引,1 行),该数组会迭代将获得的 ID 与每行的前 11 个字符进行比较。
我想知道是否有另一种方法/容器可以在搜索上产生更好的性能...
https://github.com/israpps/HDL-Batch-installer/blob/main/Database/gamename.csv
Last year I created a WxWidgets GUI to install ps2 games Into ps2 APA hard drive.
One of the features of this program consists on assigning the original game title to the game while installing the game.
To achieve this, I need both the game title and it's region code (Which follows this naming: ABCD_123.45
)
The database currently has 15701
entries disposed in a CSV.
The program currently holds this database as a std::string
array (1 index, 1 line), Which is iterated comparing the obtained ID with the first 11 chars of each line.
I was wondering if there is another method/container that can yield a better performance on the search...
https://github.com/israpps/HDL-Batch-installer/blob/main/Database/gamename.csv
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。与您当前使用的线性搜索相比,映射将具有更渐近有效的查找算法。
标准库有
std::map
和std::unordered_map
这是简单的选项,但还有其他数据结构,例如前缀尝试和有序数组。当您不需要排序顺序并且键具有标准哈希函数(字符串需要)时,std::unordered_map 通常是合理的基本选择。在实践中,根据您的问题规模,渐近有效的解决方案是否更快,您可以通过测量来发现。
Yes. A map would have more asymptotically efficient lookup algorithm compared to the linear search that you currently use.
The standard library has
std::map
andstd::unordered_map
which are easy options, but there are other data structures too such as prefix tries and ordered arrays.std::unordered_map
is usually a reasonable base choice when you don't need sorted order and when the key has a standard hash function (strings do).Whether the more asymptotically efficient solution is faster with your problem size in practice, you'll find by measuring.