Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.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++ C++;将元素插入向量_C++_Algorithm_C++11 - Fatal编程技术网

C++ C++;将元素插入向量

C++ C++;将元素插入向量,c++,algorithm,c++11,C++,Algorithm,C++11,我试图使用存储每个元素的向量来构建优先级队列。首先,我想将元素插入到具有优先级的向量中。我不知道这是否可能,如果不可能,有人能给我另一个解决办法。 这是我的密码: template <typename E> class PriorityQueue { private: std::vector<E> elements; E value; int pr; public: PriorityQueue() {} void insert(int

我试图使用存储每个元素的向量来构建优先级队列。首先,我想将元素插入到具有优先级的向量中。我不知道这是否可能,如果不可能,有人能给我另一个解决办法。 这是我的密码:

template <typename E>
class PriorityQueue {
private:
    std::vector<E> elements;
    E value;
    int pr; 

public:
  PriorityQueue() {}

  void insert(int priority, E element) {


  }   
};
模板
类优先级队列{
私人:
std::向量元素;
E值;
国际公共关系;
公众:
优先级队列(){}
无效插入(整数优先级,E元素){
}   
};
执行此操作的标准算法(见第6章)如下所示:

  • 推送项目时,将其插入向量的末尾,然后将其“起泡”到正确的位置

  • 弹出最小的项目时,将第一个项目(位置0)替换为末尾的项目,然后将其“气泡”到正确的位置

  • 可以证明这可以通过(摊销)对数时间来完成(摊销是由于向量本身可能翻倍)

    但是,不需要自己实现,因为标准库包含一个容器适配器,它使用
    std::vector
    作为默认序列容器。例如,如果您定义

    std::priority_queue<int> q;
    
    std::优先级队列q;
    

    然后,
    q
    将是一个优先级队列,用于调整向量。

    以下是如何为向量创建具有优先级的元素:

     struct PriElement
        {
            int data;
            int pri;
            bool operator < ( const PriElement & other ) const
            {
                return pri < other.pri;
            }
        };
    
        vector<PriElement> _vector;
    
    struct-PriElement
    {
    int数据;
    int pri;
    布尔运算符<(常量元素和其他)常量
    {
    返回pri
    然而,真正的问题是保持向量按优先级排序

    下面是一个简单的实现,显示了冒泡方法:

    class PriorityQueue{
    
    public:
        void insert( int data, int pri )
        {
            _vector.push_back(PriElement(data,pri));
            int index = _vector.size() -1;
            while ( ( index > 0 )&& (_vector[index] < _vector[index-1] ) )
            {
                swap(_vector[index],_vector[index-1]);
                index--;
            }
    
        }
    private:
    
        vector<PriElement> _vector;
    };
    
    类优先级队列{
    公众:
    无效插入(整数数据,整数优先级)
    {
    _向量push_back(PriElement(data,pri));
    int index=_vector.size()-1;
    而((索引>0)和(_向量[索引]<_向量[索引-1]))
    {
    交换(_向量[index],_向量[index-1]);
    索引--;
    }
    }
    私人:
    向量_向量;
    };
    

    如前所述,对于任何实际实现,请使用优先级队列。

    使用std::priority\u queueNo magic。根据优先级搜索要插入的位置,然后再插入。@PriyeshKumar具有线性复杂性,可以按对数进行(摊销)。我建议还可以看看std::push_heap和std::pop_heapI知道优先级_队列,但我正在学习数据结构。所以我想用向量或列表来处理这个问题。谢谢你的解决方案,我非常感激。如果可能的话,我可以使用最小堆来插入,这意味着我必须在插入之前对其进行堆化。我不确定我的想法是否有意义。上面的代码就是这样做的,heapify up,但是对于向量。Heapify通常指的是应用于BST的同一进程(顺便说一句,这就是优先级队列的含义)。
    运算符