は、私は非常に私はC++イテレータのようなC#でイテレータを使用するにはどうすればよいC#のポインタ、イテレータとジェネリック
を困惑のですか?私はBegin()またはEnd()アクセサを見つけることができません、イテレータを宣言する方法を見つけることさえできません。私はIenumeratorについて読んだ。私の目標は、マージ機能を実装することです。以下は、C++で書かれたMerge関数の一部です。ほとんどの場合、整数ではなく参照型を使用することを除いて、表示されているものと同等の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>
を試しています)。 - .NETイテレータを使用すると、基になるコレクションに変更を加えることはできません
bool leftValid = left.MoveNext();
bool rightValid = right.MoveNext();
while (leftValid && rightValid)
{
if (right.Current < left.Current)
{
full.Add(right.Current);
rightValid = right.MoveNext();
}
else
{
full.Add(left.Current);
leftValid = left.MoveNext();
}
}
while (leftValid)
{
full.Add(left.Current);
leftValid = left.MoveNext();
}
while (rightValid)
{
full.Add(right.Current);
rightValid = right.MoveNext();
}
full
がIList<T>
のいくつかの並べ替えする必要があります:
[Enumerable.Concat(Of TSource)メソッド](http://msdn.microsoft.com/en-us/library/bb302894.aspx)を再実装していませんか? –
@Serge no、これはマージソートのマージ部分です。注 'if(* right <* left) ' – CodesInChaos