[问题] compare function object 的相关用法

楼主: NaiveRed (天真红)   2018-03-07 23:12:59
编译器:
Mingw-w64
用 C++11 下去编
问题(Question):
标题可能下的不太好...
先讲问题,看到几个 priority_queue 在指定自己的比较方法时的写法:
1. 这边的 compare 只是 function
std::priority_queue<Node, std::vector<Node>, bool (*)(Node, Node)>
PQ(compare)
不太理解 bool (*)(Node, Node) 这个的写法是什么,有什么名称吗?
也可以直接用 lambda expression
priority_queue<node, vector<node>, bool (*)(const node &, const node &)>
PQ([](const node &a, const node &b) -> bool {
return a.v > b.v;
});
作者: loveflames (咕啾咕啾魔法阵)   2018-03-07 23:23:00
std::function是通用的函数指标
作者: PkmX (阿猫)   2018-03-08 01:55:00
C++11之后function的type可以写成:auto (args) -> ret等价于C的 ret (args) 不过看起来好读很多举例:std::function<auto (int, int) -> bool>可以更进一步 template<typename T> using ptr = T*;这样function pointer可以写成ptr<auto (int) -> bool>

Links booklink

Contact Us: admin [ a t ] ucptt.com