Redian新闻
>
弱弱的问个C++用priority_queue定义min heap的问题
avatar
弱弱的问个C++用priority_queue定义min heap的问题# JobHunting - 待字闺中
k*t
1
知道怎么用priority_queue定义min heap, 如果heap中的元素为int, 我一般用
priority_queue, cmp> minheap, 另外再另外定义下cmp。有次面试
时,面试官问我priority_queue, cmp>中,为何需要vector
我说是个container, 其他的我就啥也不知道啦。有人知道为何c++不把这container写
死为一个类型,比如vector? 让用户自己定义container有啥优势?除了vector, 还有
其他container可以用来定义min heap么?问的问题有点弱,请轻拍
avatar
p*n
2

因为是int,所以直接用greater就好了,不用自己在写一个comparator
priority_queue, std::greater> minheap
另外再另外定义下cmp。有次面试
priority_queue is a container adapter in STL,vector is used to specify
the underlining container, you can also choose deque
even though priority_queue uses vector by default, here you have to
specify it because you are going to assign a comparator and hence all the
template parameters preceding comp need to be explicitly specified.
有人知道为何c++不把这container写
i think it is designed for maximum flexibility. for example, if you think
vector<> is not good enough to meet your specific needs and you have written
your own sequential container conforming to STL requirements, then you can
easily make a customized priority_queue based on it.
让用户自己定义container有啥优势?除了vector, 还有

【在 k*******t 的大作中提到】
: 知道怎么用priority_queue定义min heap, 如果heap中的元素为int, 我一般用
: priority_queue, cmp> minheap, 另外再另外定义下cmp。有次面试
: 时,面试官问我priority_queue, cmp>中,为何需要vector
: 我说是个container, 其他的我就啥也不知道啦。有人知道为何c++不把这container写
: 死为一个类型,比如vector? 让用户自己定义container有啥优势?除了vector, 还有
: 其他container可以用来定义min heap么?问的问题有点弱,请轻拍

avatar
k*t
3
学习啦,非常感谢哈

specify

【在 p****n 的大作中提到】
:
: 因为是int,所以直接用greater就好了,不用自己在写一个comparator
: priority_queue, std::greater> minheap
: 另外再另外定义下cmp。有次面试
: priority_queue is a container adapter in STL,vector is used to specify
: the underlining container, you can also choose deque
: even though priority_queue uses vector by default, here you have to
: specify it because you are going to assign a comparator and hence all the
: template parameters preceding comp need to be explicitly specified.
: 有人知道为何c++不把这container写

avatar
a*m
4
default是vector?这样的话插入和取第一个都是 O(n)了吧。

specify

【在 p****n 的大作中提到】
:
: 因为是int,所以直接用greater就好了,不用自己在写一个comparator
: priority_queue, std::greater> minheap
: 另外再另外定义下cmp。有次面试
: priority_queue is a container adapter in STL,vector is used to specify
: the underlining container, you can also choose deque
: even though priority_queue uses vector by default, here you have to
: specify it because you are going to assign a comparator and hence all the
: template parameters preceding comp need to be explicitly specified.
: 有人知道为何c++不把这container写

avatar
a*m
5
不对。用heap实现的话还是lgn,这样应该就可以了。
这样回答lz的问题就是heap需要随机访问元素。其它简单类型比如queue,stack都不符
合这个要求。

【在 a********m 的大作中提到】
: default是vector?这样的话插入和取第一个都是 O(n)了吧。
:
: specify

avatar
p*n
6
http://en.cppreference.com/w/cpp/container/priority_queue
default确实是vector
pq.push is equivalent to vec.push_back,then call ascend method to put it in
the correct position
for pq.erase, first swap vec.front() and vec.back(), then erase vec.back()
and call descend method to put vec.front() to the correct position.

【在 a********m 的大作中提到】
: 不对。用heap实现的话还是lgn,这样应该就可以了。
: 这样回答lz的问题就是heap需要随机访问元素。其它简单类型比如queue,stack都不符
: 合这个要求。

avatar
a*m
7
嗯。应该是。刚才想偏了。

in

【在 p****n 的大作中提到】
: http://en.cppreference.com/w/cpp/container/priority_queue
: default确实是vector
: pq.push is equivalent to vec.push_back,then call ascend method to put it in
: the correct position
: for pq.erase, first swap vec.front() and vec.back(), then erase vec.back()
: and call descend method to put vec.front() to the correct position.

avatar
h*o
8
STL 靠那么细?

【在 k*******t 的大作中提到】
: 知道怎么用priority_queue定义min heap, 如果heap中的元素为int, 我一般用
: priority_queue, cmp> minheap, 另外再另外定义下cmp。有次面试
: 时,面试官问我priority_queue, cmp>中,为何需要vector
: 我说是个container, 其他的我就啥也不知道啦。有人知道为何c++不把这container写
: 死为一个类型,比如vector? 让用户自己定义container有啥优势?除了vector, 还有
: 其他container可以用来定义min heap么?问的问题有点弱,请轻拍

avatar
k*t
9
感觉面试官自己忘了怎么用啦,哈哈。不过本质上可以理解为考察heap/priority_
queue如何实现的, 如果这样,感觉也不算过。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。