When using the STL sort algorithm on a vector, I want to pass in my own comparison function which also takes a parameter.
For example, ideally I want to do a local function declaration like:
int main() {
vector<int> v(100);
// initialize v with some random values
int paramA = 4;
bool comp(int i, int j) {
// logic uses paramA in some way...
}
sort(v.begin(), v.end(), comp);
}
However, the compiler complains about that. When I try something like:
int main() {
vector<int> v(100);
// initialize v with some random values
int paramA = 4;
struct Local {
static bool Compare(int i, int j) {
// logic uses paramA in some way...
}
};
sort(v.begin(), v.end(), Local::Compare);
}
The compiler still complains: "error: use of parameter from containing function"
What should I do? Should I make some global variables with a global comparison function..?
Thanks.
See Question&Answers more detail:os