When I use function pointer to define the set order, but the answer is Segmentation fault
Solution 1:
decltype(cmp)*
The type of cmp
is bool (int, int)
. Therefore, the above type declaration is: bool (*)(int, int)
.
Therefore, your set declaration is exactly equivalent to the following:
std::set<int, bool (*)(int, int)> mse;
Now, take a look at this declaration. Which part of this declaration results in std::set
using cmp
as the comparator? This is a trick question because, of course, the answer is "none of it".
You must use an overloaded std::set
constructor that takes an instance of the comparator as the comparison function:
std::set<int, decltype(cmp)*> mse{cmp};