Доброй ночи!
Нужно создать приоритетную очередь со своей функцией определяющей приоритет. Пробовал так:
#include <queue>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
enum P {one=1,two=2};
struct S{
P p;
string s;
};
bool criterion(const S & s1,const S & s2)
{
return s1.p < s2.p;
}
int main()
{
priority_queue<S,vector<S>,criterion> p;
}
Компилятор ругается:
test.cc: In function `int main()':
test.cc:23: error: type/value mismatch at argument 3 in template parameter list
for `template<class _Tp, class _Sequence, class _Compare> class
std::priority_queue'
test.cc:23: error: expected a type, got `criterion'
test.cc:23: error: ISO C++ forbids declaration of `p' with no type
Подскажите, пожайлуста, в чем моя ошибка и как подправить?
Здравствуйте, Аноним,
Вместо bool criterion(...), попробуй:
struct Criterion: public binary_function<S, S, bool>
{
bool operator()(const S& _Left, const S& _Right) const
{
return _Left.p < _Right.p;
}
};
А>Нужно создать приоритетную очередь со своей функцией определяющей приоритет. Пробовал так:
skip
А>А>bool criterion(const S & s1,const S & s2)
А>{
А> return s1.p < s2.p;
А>}
А>
Здравствуйте, Аноним, Вы писали:
А>Доброй ночи!
А>Нужно создать приоритетную очередь со своей функцией определяющей приоритет. Пробовал так:
А>А>#include <queue>
А>#include <string>
А>#include <vector>
А>#include <algorithm>
А>using namespace std;
А>enum P {one=1,two=2};
А>struct S{
А> P p;
А> string s;
А>};
А>bool criterion(const S & s1,const S & s2)
А>{
А> return s1.p < s2.p;
А>}
А>int main()
А>{
А> priority_queue<S,vector<S>,criterion> p;
А>}
А>
А>Компилятор ругается:
А>А>test.cc: In function `int main()':
А>test.cc:23: error: type/value mismatch at argument 3 in template parameter list
А> for `template<class _Tp, class _Sequence, class _Compare> class
А> std::priority_queue'
А>test.cc:23: error: expected a type, got `criterion'
А>test.cc:23: error: ISO C++ forbids declaration of `p' with no type
А>
А>Подскажите, пожайлуста, в чем моя ошибка и как подправить?
Попробуйте так
#include <stdafx.h>
#include <queue>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
enum P {one=1,two=2};
struct S{
P p;
string s;
};
class criterion {
bool operator () (const S & s1,const S & s2)
{return s1.p < s2.p;}
};
//bool criterion(const S & s1,const S & s2)
//{
// return s1.p < s2.p;/
//}
int main()
{
priority_queue<S,vector<S>,criterion> p;
return 0;
}
т.е. требуется, чтобы criterion был (как говорится в сообщении) функциональным объектом.