如何在此队列中找到最大值?

时间:2017-12-16 04:23:42

标签: c++11 queue

我想在这个队列中找到最大值,我该如何解决?

queue<pair<int , int> > q;
 for(int i = 1; i <= n; i++){ 
  int p; 
  cin >> p; 
  q.push(make_pair(p, i)); 
 }

1 个答案:

答案 0 :(得分:0)

我希望我能正确理解你的问题,如果没有请发表评论,请查看此代码,我希望它能满足您的需求。

queue<pair<int , int> > q; 
int max=-1;//you can use INT_MIN in case you're accepting negative numbers too 

for(int i = 1; i <= n; i++)
{ 
int p;
 cin >> p; 
//if you need to be based ONLY on user input 
if(p > max) max = p;
//if you need to be based on BOTH user input and current i
//if(p+i > max) max = p+I;
//if you need to be based on ONLY current i
//if(i > max) max = i;
q.push(make_pair(p, i)); 

}