【发布时间】:2012-02-27 10:09:24
【问题描述】:
我很困惑
如何在 C# 中像使用 C++ 迭代器一样使用迭代器?我找不到 Begin() 或 End() 访问器,我什至不知道如何声明迭代器。我已阅读有关 Ienumerator 的信息。我的目标是实现合并功能。这是我用 C++ 编写的合并函数的一部分。大多数情况下,我正在寻找与所示内容相同的 C#,但我将使用引用类型而不是整数。
void merge(vector<int>::iterator left, vector<int>::iterator right, vector<int>::iterator leftEnd, vector<int>::iterator rightEnd, vector<int>::iterator full)
{
while(left != leftEnd && right!= rightEnd) //compare left and right until the end of the vector is reached
{
if(*right < *left) //right < left so insert right to the output vector and advance the iterators
{
*full++ = *right++;
}
else //left < right so insert left to the output vector and advance the iterators
{
*full++ = *left++;
}
}
while(left != leftEnd) //copy any remaining elements into the output from left
{
*full++ = *left++;
}
}
另外,我应该使用什么集合? (目前我一直在尝试List<T>和LinkedList<T>)。
【问题讨论】:
-
你不是在重新实现Enumerable.Concat(Of TSource) Method吗?
-
@Serge 不,这是归并排序的归并部分。注意
if(*right < *left)
标签: c# c++ generics collections iterator