C++中优先级队列priority_queue中出现问题
我的代码如下,不知道什么原因,老是报错。#include<iostream>#include<queue>usingnamespacestd;typedefpair<do...
我的代码如下,不知道什么原因,老是报错。
#include<iostream>
#include<queue>
using namespace std ;
typedef pair<double,int> node;
int main()
{
priority_queue<node,vector<node>,greater<node>> q;
q.push (make_pair(66.6,1));
q.push (make_pair(11.1,3));
while (!q.empty ())
{
cout << q.top ().second << ' '<<q.top.first;
q.pop ();
}
cout << endl;
}
错误提示如下:
C:\Users\Administrator\Desktop\1.cpp(8) : error C2146: syntax error : missing ',' before identifier 'q'
C:\Users\Administrator\Desktop\1.cpp(8) : error C2065: 'q' : undeclared identifier
C:\Users\Administrator\Desktop\1.cpp(8) : error C2143: syntax error : missing '>' before ';'
C:\Users\Administrator\Desktop\1.cpp(9) : error C2228: left of '.push' must have class/struct/union type
C:\Users\Administrator\Desktop\1.cpp(10) : error C2228: left of '.push' must have class/struct/union type
C:\Users\Administrator\Desktop\1.cpp(12) : error C2228: left of '.empty' must have class/struct/union type
我已经找到:priority_queue<node,vector<node>,greater<node> > q;在最后一个>前加一个空格就可以了,但是还是不知道为什么要这样? 展开
#include<iostream>
#include<queue>
using namespace std ;
typedef pair<double,int> node;
int main()
{
priority_queue<node,vector<node>,greater<node>> q;
q.push (make_pair(66.6,1));
q.push (make_pair(11.1,3));
while (!q.empty ())
{
cout << q.top ().second << ' '<<q.top.first;
q.pop ();
}
cout << endl;
}
错误提示如下:
C:\Users\Administrator\Desktop\1.cpp(8) : error C2146: syntax error : missing ',' before identifier 'q'
C:\Users\Administrator\Desktop\1.cpp(8) : error C2065: 'q' : undeclared identifier
C:\Users\Administrator\Desktop\1.cpp(8) : error C2143: syntax error : missing '>' before ';'
C:\Users\Administrator\Desktop\1.cpp(9) : error C2228: left of '.push' must have class/struct/union type
C:\Users\Administrator\Desktop\1.cpp(10) : error C2228: left of '.push' must have class/struct/union type
C:\Users\Administrator\Desktop\1.cpp(12) : error C2228: left of '.empty' must have class/struct/union type
我已经找到:priority_queue<node,vector<node>,greater<node> > q;在最后一个>前加一个空格就可以了,但是还是不知道为什么要这样? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询