由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 弱弱的问个C++用priority_queue定义min heap的问题
相关主题
面试用C++, heap 怎么办?讨论一道题
L家coding question一问问个design的问题
请教MinHeap用STL实现leetcode 上的k way merge
interview时要用stack,queue之类的东西可以不定义直接用吗大意了,尼玛
C++里如何将一个vector转换成priority_queue到底什么是priority queue啊?
弱弱的问问hash, hashtable?请教一道C++面试题
Facebook Interview Questionspriority_queue C++ implementation
G电面的一个题问个题。
相关话题的讨论汇总
话题: vector话题: int话题: queue话题: container话题: priority
进入JobHunting版参与讨论
1 (共1页)
k*******t
发帖数: 144
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么?问的问题有点弱,请轻拍
p****n
发帖数: 69
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么?问的问题有点弱,请轻拍

k*******t
发帖数: 144
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写

a********m
发帖数: 15480
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写

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

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

p****n
发帖数: 69
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都不符
: 合这个要求。

a********m
发帖数: 15480
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.

h**o
发帖数: 548
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么?问的问题有点弱,请轻拍

k*******t
发帖数: 144
9
感觉面试官自己忘了怎么用啦,哈哈。不过本质上可以理解为考察heap/priority_
queue如何实现的, 如果这样,感觉也不算过。
1 (共1页)
进入JobHunting版参与讨论
相关主题
问个题。C++里如何将一个vector转换成priority_queue
弱问C++用heap的题能用multiset吗弱弱的问问hash, hashtable?
问个G家面经题Facebook Interview Questions
An interview question of finding the median in a moving window.G电面的一个题
面试用C++, heap 怎么办?讨论一道题
L家coding question一问问个design的问题
请教MinHeap用STL实现leetcode 上的k way merge
interview时要用stack,queue之类的东西可以不定义直接用吗大意了,尼玛
相关话题的讨论汇总
话题: vector话题: int话题: queue话题: container话题: priority