std::priority_queue with std::tuple<int,int>
Oct 14, 2015 at 11:57am UTC
Hi There! I'm having some problem at run time with this program and for all I can see, it should work. I would really appreciate any help anyone can give. The code is pasted below. The problem is that when i try to use std::priority_queue::pop() the program simply exits. I left in my debugging cout's so you can see how I determined this. Additional information, I am compiling on a redhat system with the following command:
<g++ -std=c++11 -Wall -Werror -Wextra -pedantic -g3 -DDEBUG>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include<queue>
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<tuple>
#ifdef DEBUG
#define _(args) args
#else
#define _(args)
#endif
using namespace std;
typedef tuple<int ,int ,int > triple_t;
class Compare{
public :
bool operator ()(const triple_t& a, const triple_t& b) const {
if (get<0>(a) > get<0>(b))
return true ;
else if (get<0>(b) < get<0>(a))
return false ;
else if (get<1>(b)<get<1>(a))
return false ;
else if (get<1>(b)>get<1>(a))
return true ;
else
exit(1);
}
};
typedef std::priority_queue<triple_t,std::vector<triple_t>, Compare> prioq;
int main(int argc, char * argv[])
{
string values,starting;
int fill, priority;
prioq pq;
for (int i=1;i<argc;++i)
{
ifstream file(argv[i]);
getline(file,values);
getline(file,starting);
size_t pos = starting.find(":" );
++++pos;
priority = stoi(starting.substr(pos));
istringstream ss(values);
while (ss>>fill)
{
_(cout<<priority<<" " <<i<<" " <<fill<<endl;)
pq.push(make_tuple(priority,i,fill));
priority+=fill;
}
}
while (pq.size()!=0)
{
_(cout<<"InLoop " <<pq.size()<<endl;)
cout<<get<2>(pq.top())<<endl;
_(cout<<"ONE" <<pq.size()<<endl;)
pq.pop();
_(cout<<"TWO" <<pq.size()<<endl;)
}
}
Last edited on Oct 14, 2015 at 11:58am UTC
Oct 14, 2015 at 12:30pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class Compare{
public :
bool operator ()(const triple_t& a, const triple_t& b) const {
if (get<0>(a) > get<0>(b))
return true ;
else if (get<0>(b) < get<0>(a))
return false ;
else if (get<1>(b)<get<1>(a))
return false ;
else if (get<1>(b)>get<1>(a))
return true ;
else
exit(1); // what is this???
// exit program if get<0>(a) == get<0>(b) and get<1>(a) == get<1>(b) ???
}
};
Topic archived. No new replies allowed.