我遇到了一个记录存储为的要求
Name : Employee_Id : Address
Run Code Online (Sandbox Code Playgroud)
其中Name和Employee_Id应该是键,在Name和Employee Id上都提供搜索功能.
我可以考虑使用地图来存储这个结构
std::map< std:pair<std::string,std::string> , std::string >
// < < Name , Employee-Id> , Address >
Run Code Online (Sandbox Code Playgroud)
但我不确定搜索功能的样子.
我想存储一堆键值对象,但值对象本身(及其对它的引用)知道它的键.我还想在只给出密钥的情况下有效地查找这些对象.
class SomeObject
{
private:
//String or integer. int seem cheap enough to duplicate with std::map, but
//strings seem pretty expensive when there may be thousands of objects in existence.
//Reference/Pointer to key is fine
const SomeOtherObject key;
...other stuff...
public:
...methods, some of which use the key in some way...
};
Run Code Online (Sandbox Code Playgroud)
我承认这个问题在此之前已经被问到,但是现在是4年前,我敢于要求更新:
我需要一种方法来将一个元组/对添加到容器中并有效地搜索左侧和右侧元素.
升压具有bimap和multi_index该做的正是我想要的,但我不知道什么是纯现代C++推荐的替代方案- 11/14如果你不希望引入的依赖性提高(无论何种原因).
链接中的一个答案表明不需要s.th. 由于透明的比较器,更像是一个bimap .接受的答案表明将std::maps与key1- > key2和key2- > 组合在一起的实现key1.
我真的不知道透明比较器如何帮助我,我只是好奇是否有一些这是你应该怎么做以及为什么 - 解决方案.你能提供一些提示/链接吗?