R
raylopez99
Scott Meyers in his 2001 book on STL / C++ claims that the following
code is optimal--what is the C# way and should you follow his example
and try and do this in LINQ? Pseudo code is OK, I'm just looking for
a conceptual answer.
"Suppose you have a vector <int> [this is like List<> in C#--RL] and
you'd like to get rid of all the elements in the vector whose value is
less than x, except that elements preceding the last occurrence of a
value at least as big as y should be retained"
Answer:
vector <int> v;
int x,y;
....
v.erase (remove_if (find_if (v.rbegin(), v.rend(),
bind2nd(greater_equal<int>(),y)).base(),
v.end(), bind2nd(less<int>(),x)), v.end());
Meyers goes on to claim, which I suspect some people here would agree,
that this functional type expression, which uses ten different
functions in one line, including reverse iterators, bind2nd, and
anonymous function objects, is actually more efficient (though less
readable) than trying to do it with many lines of code "by hand".
He calls such code "write only" code since only the author really
knows what's going on, and the reader is perplexed, so he recommends
lots of comments but still believes you should use the STL when
possible.
Comments?
RL
code is optimal--what is the C# way and should you follow his example
and try and do this in LINQ? Pseudo code is OK, I'm just looking for
a conceptual answer.
"Suppose you have a vector <int> [this is like List<> in C#--RL] and
you'd like to get rid of all the elements in the vector whose value is
less than x, except that elements preceding the last occurrence of a
value at least as big as y should be retained"
Answer:
vector <int> v;
int x,y;
....
v.erase (remove_if (find_if (v.rbegin(), v.rend(),
bind2nd(greater_equal<int>(),y)).base(),
v.end(), bind2nd(less<int>(),x)), v.end());
Meyers goes on to claim, which I suspect some people here would agree,
that this functional type expression, which uses ten different
functions in one line, including reverse iterators, bind2nd, and
anonymous function objects, is actually more efficient (though less
readable) than trying to do it with many lines of code "by hand".
He calls such code "write only" code since only the author really
knows what's going on, and the reader is perplexed, so he recommends
lots of comments but still believes you should use the STL when
possible.
Comments?
RL