在 C# 中编写 C++ std::反向等效的最接近的方法是什么?

What is the closest way of writing a C++ std::reverse equivalent in C#?

本文关键字:最接近 是什么 方法 std C++      更新时间:2023-10-16

C++标准库中的reverse算法相当于

template <class BidirectionalIterator>
  void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
  while ((first!=last)&&(first!=--last)) {
    std::iter_swap (first,last);
    ++first;
  }
}

据 http://www.cplusplus.com/reference/algorithm/reverse/说。我想用 C# 编写等效项:

public void Reverse<T> ( T first, T last ) 
{
    // ... 
}

首先,你如何传递两个对IEnumerator的引用,第二个是可以向后移动的引用?有没有一种自然的方法可以做到这一点,还是需要先延长IEnumerable<T>

其次,如果上一个问题的答案是否定的,那么是否有一种经过 Skeet 认证的 C# 编写reverse方式,在通用性和有效性方面与C++方式相当?如果是这样,它是什么?

List<T> 接口提供了一个Reverse()方法,该方法将与等效C++相同。

看看 https://msdn.microsoft.com/en-us/library/b0axc2h2(v=vs.110).aspx