The reverse
algorithm in the C++ standard library is equivalent to
template <class BidirectionalIterator>
void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
while ((first!=last)&&(first!=--last)) {
std::iter_swap (first,last);
++first;
}
}
according to http://www.cplusplus.com/reference/algorithm/reverse/. I want to write the equivalent in C#:
public void Reverse<T> ( T first, T last )
{
// ...
}
First of all, how do you pass in two references to IEnumerator
s, with the second being one that can travel backwards? Is there a natural way of doing this or does it require first extending IEnumerable<T>
?
Second of all, if the answer to the previous question is no, then is there a Skeet-certified C# way of writing reverse
that equals the C++ one in terms of generality and effeciency? If so, what is it?