关于set容器的排序问题,黑马教的用仿函数。我就想试试将set<>中的第二个数据类型参数由自定义类换成换成函数指针类看看能不能行。函数体一样返回bool值,一样是定义出的数据与()发生反应,而且编译器没报错,但最后运行不出来。(异常看不懂)
还有个问题,仿函数()重载为啥不加const就会报错?
构造set时要把compare作为参数传入.
#include <iostream>
#include <set>
#include <string>
using cpr = struct
{
int a;
int b;
};
auto Less(const cpr &a, const cpr &b) -> bool;
auto main() -> int
{
cpr arr[] = {{1, 2}, {3, 4}};
std::set<cpr, decltype(Less) *> test(Less);
test.insert(arr, arr + 2);
for (const auto &i : test)
{
std::cout << i.a << std::endl;
}
return 0;
}
auto Less(const cpr &aa, const cpr &bb) -> bool
{
return aa.a < bb.b;
}
因为他重载的不是一个变量,const代表的是常量,是不能变的
代码贴出来