Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #10513 > unrolled thread

Issue with STL ( priority Queue)

Started bynishit gupta <nishit.gupta@nsitlounge.in>
First post2011-09-26 04:48 -0700
Last post2011-09-26 13:58 +0200
Articles 2 — 2 participants

Back to article view | Back to comp.lang.c++


Contents

  Issue with STL ( priority Queue) nishit gupta <nishit.gupta@nsitlounge.in> - 2011-09-26 04:48 -0700
    Re: Issue with STL ( priority Queue) Thomas Boell <tboell@domain.invalid> - 2011-09-26 13:58 +0200

#10513 — Issue with STL ( priority Queue)

Fromnishit gupta <nishit.gupta@nsitlounge.in>
Date2011-09-26 04:48 -0700
SubjectIssue with STL ( priority Queue)
Message-ID<29cdc97e-972b-4ad5-9d08-edcccea287b1@b6g2000vbz.googlegroups.com>
If the values being thrown in priority queue have same values then I
expect them to come out (when popped) in order in which they were
thrown, but this is not the case, i.e. with below code
##################################################################################
#include <iostream>
#include  <queue>
using namespace std;

class my_class {
    public:
	int val;
	char ID;
	my_class(int tmp, int tmp_id ):val(tmp), ID(tmp_id){}
};


struct comparison {
    bool operator() (const my_class left, const my_class right) const
{
	return left.val < right.val;
    }
};


int main(){
    std::priority_queue < my_class, std::deque<my_class >,comparison
> obj;
    obj.push(my_class(5, 'A'));
    obj.push(my_class(5, 'B'));
    obj.push(my_class(5, 'C'));
    obj.push(my_class(5, 'D'));
    for(unsigned i=0; i<4; i++){
	cout<<"\n val = "<<obj.top().val<<"  ID = "<<obj.top().ID<<endl;
	obj.pop();
    }
}
##########################################################################################
The output comes :
 val = 5  ID = A
 val = 5  ID = C
 val = 5  ID = B
 val = 5  ID = D
But I expected the ouput as :
val = 5  ID = A
 val = 5  ID = B
 val = 5  ID = C
 val = 5  ID = D


Any explanations for this?
Thanks in advance

[toc] | [next] | [standalone]


#10514

FromThomas Boell <tboell@domain.invalid>
Date2011-09-26 13:58 +0200
Message-ID<20110926135842.30808283@arcadia>
In reply to#10513
On Mon, 26 Sep 2011 04:48:35 -0700 (PDT)
nishit gupta <nishit.gupta@nsitlounge.in> wrote:

> If the values being thrown in priority queue have same values then I
> expect them to come out (when popped) in order in which they were
> thrown, but this is not the case, i.e. with below code

No, it is not the case. 
If you want your IDs to appear in a particular order, you have to
include them in your comparison function.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web