priority_queue 第二个参数:容器对象
priority_queue second parameter :Container object
我知道 priority_queue 的第二个参数是一个容器(默认情况下是 vector
)。但是,如果我想使用 queue
或 list
作为底层容器呢?
struct compare {
bool operator()(int&a,int&b) {
return a > b;
}
};
int main(int argc, char** argv) {
std::priority_queue<int, queue<int> , compare >pq();
return 0;
}
当我使用 pq.push()
时它不起作用。
那么priority_queue
一定要用vector
作为容器吗?或者我怎样才能改用队列?
非常感谢你的帮助。非常感谢。
从这个reference:
Container - The type of the underlying container to use to store the elements. The
container must satisfy the requirements of SequenceContainer and its iterators must satisfy the requirements of RandomAccessIterator.
Additionally, it must provide the following functions with the usual
semantics:
- front()
- push_back()
- pop_back()
The standard containers std::vector and std::deque satisfy these
requirements.
我知道 priority_queue 的第二个参数是一个容器(默认情况下是 vector
)。但是,如果我想使用 queue
或 list
作为底层容器呢?
struct compare {
bool operator()(int&a,int&b) {
return a > b;
}
};
int main(int argc, char** argv) {
std::priority_queue<int, queue<int> , compare >pq();
return 0;
}
当我使用 pq.push()
时它不起作用。
那么priority_queue
一定要用vector
作为容器吗?或者我怎样才能改用队列?
非常感谢你的帮助。非常感谢。
从这个reference:
Container - The type of the underlying container to use to store the elements. The container must satisfy the requirements of SequenceContainer and its iterators must satisfy the requirements of RandomAccessIterator. Additionally, it must provide the following functions with the usual semantics:
- front()
- push_back()
- pop_back()
The standard containers std::vector and std::deque satisfy these requirements.