Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.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++_Constants_Priority Queue_Const Correctness - Fatal编程技术网

C++ 访问优先级队列c+中的第一个元素+;

C++ 访问优先级队列c+中的第一个元素+;,c++,constants,priority-queue,const-correctness,C++,Constants,Priority Queue,Const Correctness,我正在编写一个程序,要求我使用优先级队列。根据我的理解,优先级队列会自动将队列从最大元素排序到最小元素。我创建了一个简单的具有名称和id号的对象(节点)优先级队列。我试图访问队列中的第一个对象,所以我想我可以只使用“front”成员函数。这在使用标准队列时有效,但在使用优先级队列时会出现错误 错误:“class std::priority_queue”没有名为“front”的成员 我也尝试过使用“top”,但后来我发现了错误 错误:将'const value_type(aka const nod

我正在编写一个程序,要求我使用优先级队列。根据我的理解,优先级队列会自动将队列从最大元素排序到最小元素。我创建了一个简单的具有名称和id号的对象(节点)优先级队列。我试图访问队列中的第一个对象,所以我想我可以只使用“front”成员函数。这在使用标准队列时有效,但在使用优先级队列时会出现错误

错误:“class std::priority_queue”没有名为“front”的成员

我也尝试过使用“top”,但后来我发现了错误

错误:将'const value_type(aka const node)'作为'this'参数传递 'void::display()'的类型将丢弃限定符[-fpermissive]

这是我的密码:

#include <iostream>       
#include <queue>          

using namespace std;

struct node
{
    node(char n, int i) { name = n; id = i; }
    void display() { cout << "name: " << name << "   id : " << id << endl; }
    friend bool operator < (node a, node b) { return a.id < b.id; }
private:
    char name;
    int id;
};

int main()
{
  queue<node> myqueue; // Actually want a priority_queue<node>

  myqueue.push(node('a',5));
  myqueue.push(node('b',9));
  myqueue.push(node('c',7));

  myqueue.front().display(); // Error when using the type I want, even if I call top() instead
}
#包括
#包括
使用名称空间std;
结构节点
{
节点(charn,inti){name=n;id=i;}

void display(){cout首先,
std::priority\u queue::front
不存在。我们无法调用不存在的东西

其次,保持常量正确。声明您的成员函数
const

void display() const
{...}

这是因为
std::priority_queue::top的返回类型是常量引用,只能使用常量成员函数。在常量成员函数中,对象不能修改,这就是常量的含义。

首先,
std::priority\u queue::front
不存在。我们无法调用不存在的东西

其次,保持常量正确。声明您的成员函数
const

void display() const
{...}

这是因为
std::priority\u queue::top的返回类型是常量引用。通过常量引用(和常量值),只能使用常量成员函数。在常量成员函数中,无法修改对象,这就是常量的含义。

您的错误是
.display()
不是一个常量成员函数。
应该是:

void display() const { std::cout << "name: " << name << "   id : " << id << '\n';}

void display()const{std::cout您的错误是
.display()
不是常量成员函数。
应该是:

void display() const { std::cout << "name: " << name << "   id : " << id << '\n';}

void display()常量{std::coutstd::priority\u queue::front不存在。
使用

是的,函数display()应该是const,正如其他成员所提到的:)


void display()常量{std::coutstd::priority\u queue::front不存在。
使用

是的,函数display()应该是const,正如其他成员所提到的:)


void display()常量{std::cout发生在每个人身上。我澄清了这个问题,但除非你找到一种方法来添加其他答案中没有的内容,否则保留此内容不会有多大用处……事实上,我错过了问题的顶部,发现没有人谈论每个人都会遇到这种情况。我澄清了这个问题,但除非你找到一种方法来添加其他内容,而不是其他答案L在其他答案中,保留这一点没有多大用处……事实上,我错过了问题的第一部分,发现没有人在谈论这一点。好吧,你为写更多关于常数正确性的文章而获得了一张赞成票。好吧,你为写更多关于常数正确性的文章而获得了一张赞成票。