Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 带优先级队列的单独链接(使用std::map)_C++_C++11_Hashmap_Hashtable_Priority Queue - Fatal编程技术网

C++ 带优先级队列的单独链接(使用std::map)

C++ 带优先级队列的单独链接(使用std::map),c++,c++11,hashmap,hashtable,priority-queue,C++,C++11,Hashmap,Hashtable,Priority Queue,我刚开始学习哈希表,在尝试使用std::map时,我想到了一个问题:当使用单独的链接方法来解决冲突时,我是否可以使用std::priority\u queue而不仅仅是list 例如,有一大群人,我有他们的名字和年龄的信息,我想得到的是有相同名字的人的排序列表,例如根据他们的年龄排列的“David” 因此,要做到这一点,我首先使用他们的名字作为关键,将这些人放入地图,然后使用std::priority_队列根据年龄解决导致冲突的同名人 这是解决这个问题的正确方法吗? 我只是意识到我并不真正了解s

我刚开始学习哈希表,在尝试使用std::map时,我想到了一个问题:当使用单独的链接方法来解决冲突时,我是否可以使用
std::priority\u queue
而不仅仅是list

例如,有一大群人,我有他们的名字和年龄的信息,我想得到的是有相同名字的人的排序列表,例如根据他们的年龄排列的“David”

因此,要做到这一点,我首先使用他们的名字作为关键,将这些人放入地图,然后使用std::priority_队列根据年龄解决导致冲突的同名人

这是解决这个问题的正确方法吗? 我只是意识到我并不真正了解
std::map
背后的奥秘,是使用单独的链接还是线性探测来解决冲突?我找不到答案

我所描述的问题的简单代码可能有助于澄清问题:

class people {

public:
people(string inName, int inAge):firstName(inName), age(inAge){};
private:

string firstName;
int age;


}

int main(int argc, char ** argv) {

string name;
int age;

name = "David";
age = 25;
people  aPerson(name, age);
//This is just an example, there are usually more than two attributes to deal with.


std::map <string, people> peopleList;

peopleList[name] = aPerson;

//now how do I implement the priority queue for collision first names? 
}
班级人员{
公众:
人(字符串inName,inAge):名字(inName),年龄(inAge){};
私人:
字符串名;
智力年龄;
}
int main(int argc,字符**argv){
字符串名;
智力年龄;
name=“大卫”;
年龄=25岁;
人(姓名、年龄);
//这只是一个例子,通常需要处理两个以上的属性。
地图人物列表;
peopleList[姓名]=个人;
//现在,如何实现冲突名字的优先级队列?
}
提前谢谢


编辑:因为我需要O(1)搜索,所以我应该使用无序地图而不是地图。

现在,您有一个名称和单个
对象之间的映射。您需要将映射更改为名称和
std::priority_队列
之间的映射,并为优先级队列使用自定义比较器:

auto comparator = [](const people& p1, const people& p2) -> bool
    { return (p1.age < p2.age); }

std::map<std::string,
         std::priority_queue<people, std::vector<people>, comparator>> peopleList;

// ...

peopleList[name].push(aPerson);
autocomparator=[](constpeople&p1,constpeople&p2)->bool
{返回(p1.age
std::map
根本不是散列,它是一个二叉树。此外,您还可以为复合
(名称、年龄)
键编写一个比较器,而无需使用嵌套容器。由于map是一个二叉树,因此需要O(1)进行搜索,它不满足此条件?
std::map
是排序的,是搜索和插入的对数时间
std::unordered_map
顾名思义是未排序的,搜索和插入的时间是固定的,并且实现为散列。Thanks很多,对于unordered map也是一样的,对吗?