gpt4 book ai didi

具有两个搜索条件的 C++ 关联容器

转载 作者:行者123 更新时间:2023-11-30 00:38:49 25 4
gpt4 key购买 nike

我正在寻找一个容器来存储我的类对象,称为 Link,它有一个成员变量:“std::string linkID”。容器的大小可以高达数百万,因此更喜欢快速查找。1-稍后我需要使用 linkID 查找容器2-但是,由于某些原因,添加对象的顺序也很重要。

我学习了 vector、list、set、map... 我打算学习 muliset、multimap 和 boost multi index。但我想,作为 C++ 的新手,我仍然需要一些更有经验的意见来帮助我根据以上两个标准选择最佳容器。

谢谢瓦赫德

最佳答案

对我来说,这看起来是 boost::multi_index_container 的完美用例

你的类型声明应该看起来与此类似(未测试)

typedef multi_index_container<
Link,
indexed_by<
sequenced<>,
ordered_unique< member< Link, std::string, &Link::linkID> >,
ordered_unique< member< Link, std::string, &Link::linkID_2 > >
>
> LinkList;
LinkList s;
s.push_back(Link(...));

它通常与 Frerich Raabe 发布的解决方案相同。但我认为陷阱更少,可能更优化。阅读 tutorial在 boost 页面上学习如何访问存储在 s 中的元素。

你的评论...

Actually, I am developing an already existing project where in, theyhave used std::vector for only STORING the elements(as you pointedout). so now can I use the 'storage' vector as is and just add themaps as you suggested?

这是不可能的。 vector 迭代器保持无效。当您向 vector 中添加或删除元素时,它们可能会变为未定义。

关于具有两个搜索条件的 C++ 关联容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9838074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com