社区 C++ 语言
interim2012 2012-03-31 04:05:38
main.cpp:31: error: type/value mismatch at argument 3 in template parameter list for 'template<class _Tp, class _Sequence, class _Compare> class std::priority_queue'
main.cpp:31: error: expected a type, got 'cmp'
template < class T, class Container = vector<T>,
class Compare = less<typename Container::value_type> > class priority_queue;
Compare: Comparison class: A class such that the expression comp(a,b), where comp is an object of this class and a and b are elements of the container, returns true if a is to be placed earlier than b in a strict weak ordering operation. This can either be a class implementing a function call operator or a pointer to a function.
不是可以用函数指针吗?为什么我的程序还会出错
#include<iostream>
#include<functional>
#include<queue>
using namespace std;
struct node
{
// friend bool operator< (node n1, node n2)
// {
// return n1.priority < n2.priority;
// }
int priority;
int value;
};
//第一种是正确的
//struct cmp{
// bool operator() ( node a, node b ){
// return a.priority < b.priority;
// }
//};
//这种出错了
bool cmp ( node a, node b )
{
return a.priority < b.priority;
} int main()
{
priority_queue<node, vector<node>, cmp> qn; ///error: expected a type, got 'cmp'
node b[len];
b[0].priority = 6; b[0].value = 1;
b[1].priority = 9; b[1].value = 5;
b[2].priority = 2; b[2].value = 3;
b[3].priority = 8; b[3].value = 2;
b[4].priority = 1; b[4].value = 4;
for(i = 0; i < len; i++)
qn.push(b[i]);
cout<<"优先级"<<'\t'<<"值"<<endl;
for(i = 0; i < len; i++)
{
cout<<qn.top().priority<<'\t'<<qn.top().value<<endl;
qn.pop();
}
return 0;
}
...全文
607 12
打赏 收藏
分享
转发到动态
举报
AI 作业写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
www_adintr_com 2012-04-01
priority_queue <node, vector <node> , cmp> qn;
等价于
priority_queue <node, vector <node> , cmp> qn(cmp());
使用了默认参数。
interim2012 2012-03-31
谢谢 那这种又是什么用法呢?我只知道cmp类重载了()
struct cmp{
bool operator() ( node a, node b ){
return a.priority < b.priority;
}
};
priority_queue<node, vector<node>, cmp> qn;
传了cmp 这种类型进去,为什么就能实现优先级对列呢
www_adintr_com 2012-03-31
是的.
interim2012 2012-03-31
[Quote=引用 8 楼 的回复:] 使用函数对象的完整写法其实也应该是这样: cmp cmp_obj(...); 当 cmp 这个类有默认构造函数时可以有你那种简写的方法, 如果没有默认构造函数就需要像上面那样写了.
priority_queue<node, vector<node>, cmp> qn(cmp_obj);
[/Quote]
cmp cmp_obj(...);
priority_queue<node, vector<node>, cmp> qn(cmp_obj);
我查了下Priority_queue的构造函数。
xplicit priority_queue ( const Compare& x = Compare(),
const Container& y = Container() );
这种用法:qn(cmp_obj);
是不是就是调用了这个构造函数,来初始化x
www_adintr_com 2012-03-31
使用函数对象的完整写法其实也应该是这样: cmp cmp_obj(...); 当 cmp 这个类有默认构造函数时可以有你那种简写的方法, 如果没有默认构造函数就需要像上面那样写了.
priority_queue<node, vector<node>, cmp> qn(cmp_obj);
www_adintr_com 2012-03-31
template < class T, class Container = vector<T>, Compare 是一个类型参数啊, 你只能传递一个类型给它, 不能把一个函数指针给它呀.
class Compare = less<typename Container::value_type> > class priority_queue;
模版参数有两类, 一类是类型, 一类是整数. 函数指针算是整数的, 不能传给接收类型的.
interim2012 2012-03-31
上面的代码有错。重新贴下代码 priority_queue<node, vector<node>, cmp> qn;这种用法错了 bool cmp ( node a, node b ) int main() for(i = 0; i < len; i++)
下面这种对了。但不知道为什么要像下面这样子去用
priority_queue<node, vector<node>, bool(*)(node, node)> qn(cmp);
#include<iostream>
#include<functional>
#include<queue>
using namespace std;
struct node
{
// friend bool operator< (node n1, node n2)
// {
// return n1.priority < n2.priority;
// }
int priority;
int value;
};
//struct cmp{
// bool operator() ( node a, node b ){
// return a.priority < b.priority;
// }
//};
{
return a.priority < b.priority;
}
{
int len = 5;
int i;
//示例3
// priority_queue<node> qn;
// priority_queue<node, vector<node>, cmp> qn;
priority_queue<node, vector<node>, bool(*)(node, node)> qn(cmp);
node b[len];
b[0].priority = 6; b[0].value = 1;
b[1].priority = 9; b[1].value = 5;
b[2].priority = 2; b[2].value = 3;
b[3].priority = 8; b[3].value = 2;
b[4].priority = 1; b[4].value = 4;
qn.push(b[i]);
cout<<"优先级"<<'\t'<<"值"<<endl;
for(i = 0; i < len; i++)
{
cout<<qn.top().priority<<'\t'<<qn.top().value<<endl;
qn.pop();
}
return 0;
}
interim2012 2012-03-31
[Quote=引用 2 楼 的回复:] 用函数指针的话你需要这样使用: C/C++ code priority_queue<node, vector<node>, bool(*)(node, node)> qn(cmp); 谢谢。
[/Quote]
但不明白用法为什么是这样?
Saingel 2012-03-31
[Quote=引用 3 楼 的回复:] C/C++ code struct cmp
{
bool operator ()(const node& n1, const node& n2) const
{
return n1.priority < n2.priority;
}
};
[/Quote]看错 - - 还以为是map 无视我把
Saingel 2012-03-31
struct cmp
{
bool operator ()(const node& n1, const node& n2) const
{
return n1.priority < n2.priority;
}
};
www_adintr_com 2012-03-31
用函数指针的话你需要这样使用: priority_queue<node, vector<node>, bool(*)(node, node)> qn(cmp);
ouyh12345 2012-03-31
struct node
{
bool operator< (const node &n1, const node &n2) const
{
return n1.priority < n2.priority;
}
int priority;
int value;
};
au3反编译源码
Expected a type 的错误
在定义一个协议的时候,出现Expected a type 的错误?解决办法:#import <UIKit/UIKit.h>转载于:https://www.cnblogs.com/yaoyao0110/p/4793978.html
【CS Round #43 A】Expected Dice
【链接】https://csacademy.com/contest/round-43/task/expected-dice/【题意】大水题【题解】把36种可能的结果都存下来.然后把重复出现的次数最多的输出就好了。【错的次数】0【反思】在这了写反思【代码】#include<bits/stdc++.h>us...
2.python/pytorch编程debug
快速解决python中的bug,这里总有你需要的。
[转]python模块全面
python模块http://www.cnblogs.com/wupeiqi/articles/4963027.html模块概念:用一砣代码实现了某个功能的代码集合。类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合。而对于一个复杂的功能来,可能需要多个函数才能完成(函数又可以在不同的.py文件中),n个 .py 文...