방법: 제네릭 컬렉션과 각각에 대해 반복
제네릭(C++ 구성 요소 확장) Visual C++의 기능을 사용 하면 제네릭 컬렉션을 만들 수 있습니다.
예제
이 샘플을 사용 하는 방법을 보여 줍니다. for each 간단한 제네릭 값 형식의 컬렉션입니다.
// for_each_generics.cpp
// compile with: /clr
using namespace System;
using namespace System::Collections::Generic;
generic <class T>
public value struct MyArray : public IEnumerable<T> {
MyArray( array<T>^ d ) {
data = d;
}
ref struct enumerator : IEnumerator<T> {
enumerator( MyArray^ myArr ) {
colInst = myArr;
currentIndex = -1;
}
virtual bool MoveNext() = IEnumerator<T>::MoveNext {
if ( currentIndex < colInst->data->Length - 1 ) {
currentIndex++;
return true;
}
return false;
}
virtual property T Current {
T get() {
return colInst->data[currentIndex];
}
};
property Object^ CurrentNonGeneric {
virtual Object^ get() = System::Collections::IEnumerator::Current::get {
return colInst->data[currentIndex];
}
};
virtual void Reset() {}
~enumerator() {}
MyArray^ colInst;
int currentIndex;
};
array<T>^ data;
virtual IEnumerator<T>^ GetEnumerator() {
return gcnew enumerator(*this);
}
virtual System::Collections::IEnumerator^ GetEnumeratorNonGeneric() = System::Collections::IEnumerable::GetEnumerator {
return gcnew enumerator(*this);
}
};
int main() {
MyArray<int> col = MyArray<int>( gcnew array<int>{10, 20, 30 } );
for each ( Object^ c in col )
Console::WriteLine((int)c);
}