Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.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++11 如何强制重组优先级队列?_C++11_Priority Queue - Fatal编程技术网

C++11 如何强制重组优先级队列?

C++11 如何强制重组优先级队列?,c++11,priority-queue,C++11,Priority Queue,我有一个priority\u队列,它包含一个带有一些对象的向量 std::priority_queue<std::shared_ptr<Foo>, std::vector<std::shared_ptr<Foo>>, foo_less> foo_queue; std::优先级队列foo\u队列; 它有一个foo_队列函数,可以对优先级队列进行排序 现在,在priority\u队列之外,我想更改一些必须影响priority\u队列顺序的对象值 我

我有一个
priority\u队列
,它包含一个带有一些对象的
向量

std::priority_queue<std::shared_ptr<Foo>, std::vector<std::shared_ptr<Foo>>, foo_less> foo_queue;
std::优先级队列foo\u队列;
它有一个
foo_队列
函数,可以对
优先级队列
进行排序

现在,在
priority\u队列
之外,我想更改一些必须影响
priority\u队列
顺序的对象值

我的问题是:


如何设置某种“刷新”来触发
priority\u队列
运行
foo\u队列()
,以便始终保持其有序性?

使用标准堆算法和 矢量。如果要更改密钥,请从中查找并删除该值 基础向量并调用该向量上的make_heap。改变钥匙,然后 把它推回到堆上。所以成本是向量的线性搜索 查找值并调用make_heap(我认为这也是线性的)

#包括
#包括
#包括
模板
将my_priority_队列分类{
受保护的:
容器c;
比较comp;
公众:
显式my_priority_队列(const Container&c_u=Container(),
const Compare&comp\ux=Compare())
:c(c_),comp(comp_)
{
std::make_heap(c.begin(),c.end(),comp);
}
bool empty()常量{返回c.empty();}
std::size_t size()常量{返回c.size();}
const T&top()const{return c.front();}
无效推送(常数T&x)
{
c、 推回(x);
std::push_堆(c.begin(),c.end(),comp);
}
void pop()
{
std::pop_堆(c.begin(),c.end(),comp);
c、 向后弹出();
}
无效删除(常数T&x)
{
autoit=std::find(c.begin(),c.end(),x);
如果(it!=c.end()){
c、 抹去(它);
std::make_heap(c.begin(),c.end(),comp);
}
}
};
福班{
int x_2;;
公众:
Foo(intx):x_ux{}

bool操作员“有影响力”不太简单,但我喜欢它!为什么不删除对象,更新它的值,然后把它添加进去?哦,等等,那可能不像看起来那么容易……我偶然地把我的母语和英语混在一起……opcSIT比看起来更容易,因为C++标准文档包含这样的一个示例实现,我没有PU!在所有的细节中都没有,但是如果你需要更多的功能(系数,等等),你可以去那里看看。我试图看看我们是否可以在标准实现中找到基础向量,但我不知道如何做到这一点(虽然我不明白为什么这是不可能的,所以我可能遗漏了一些东西)。
#include <iostream>
#include <vector>
#include <algorithm>

template <class T, class Container = std::vector<T>,
          class Compare = std::less<T> >
class my_priority_queue {
protected:
    Container c;
    Compare comp;
public:
    explicit my_priority_queue(const Container& c_  = Container(),
                            const Compare& comp_ = Compare())
        : c(c_), comp(comp_)
    {
        std::make_heap(c.begin(), c.end(), comp);
    }
    bool empty()       const { return c.empty(); }
    std::size_t size() const { return c.size(); }
    const T& top()     const { return c.front(); }
    void push(const T& x)
    {
        c.push_back(x);
        std::push_heap(c.begin(), c.end(), comp);
    }
    void pop()
    {
        std::pop_heap(c.begin(), c.end(), comp);
        c.pop_back();
    }
    void remove(const T& x)
    {
        auto it = std::find(c.begin(), c.end(), x);
        if (it != c.end()) {
            c.erase(it);
            std::make_heap(c.begin(), c.end(), comp);
        }
    }
};

class Foo {
    int x_;
public:
    Foo(int x) : x_(x) {}
    bool operator<(const Foo& f) const { return x_ < f.x_; }
    bool operator==(const Foo& f) const { return x_ == f.x_; }
    int get() const { return x_; }
    void set(int x) { x_ = x; }
};

int main() {
    my_priority_queue<Foo> q;

    for (auto x: {7, 1, 9, 5}) q.push(Foo(x));
    while (!q.empty()) {
        std::cout << q.top().get() << '\n';
        q.pop();
    }

    std::cout << '\n';

    for (auto x: {7, 1, 9, 5}) q.push(Foo(x));
    Foo x = Foo(5);
    q.remove(x);
    x.set(8);
    q.push(x);
    while (!q.empty()) {
        std::cout << q.top().get() << '\n';
        q.pop();
    }
}