303 karma · joined Jul 19, 2013 (13y ago) · 145 submissions
comment 4y ago · link · parent
// A. Using a struct. The old manual way.
struct my_lambda {
my_lambda(int c) : c_(c) {}
int operator() (int x) const {
return x == c_;
}
int c_;
};
auto f(vector<int> numbers) {
my_lambda lam(2);
// remove all the matching elements
erase_if(numbers, lam);
return numbers;
}
// B. Using a lambda
auto g(vector<int> numbers) {
int c = 2;
erase_if(numbers, [c](int x) { return x == c; });
return numbers;
}comment 4y ago · link · parent
comment 6y ago · link · parent
comment 7y ago · link · parent
comment 7y ago · link · parent
Indirection in C and C++ is a mess, but at least C has the "restrict" keyword. Best to program with value types whenever you can, and use pointers and references when you must.
comment 7y ago · link · parent
comment 8y ago · link · parent
K-means and other heuristic algorithms fit that description.
comment 8y ago · link · parent