Visual C++ .NET/Collections/IEnumerator

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Display elements in a multiset by IEnumerator<> interface

<source lang="csharp">

  1. include "stdafx.h"
  2. include <cliext/set>

using namespace System; using namespace cliext; using namespace System::Collections::Generic; ref class MyClass{ public:

   String^ Name;
   MyClass() : Name(String::Empty) { }
   MyClass(String^ name) : Name(name) { }
   MyClass(const MyClass% orig){
       Name = orig.Name; 
   }
   MyClass% operator=(const MyClass% orig){
       if (this != %orig)
           Name = orig.Name;
       return *this;
   }
   ~MyClass() { }
   bool operator<(const MyClass^ rhs){
       return (Name->CompareTo(rhs->Name) < 0);
   }
   bool operator==(const MyClass^ rhs){
       return (Name->Equals(rhs->Name));
   }

}; int main(array<System::String ^> ^args) {

   multiset<MyClass^> mpets; 
   mpets.insert(gcnew MyClass("King")); 
   mpets.insert(gcnew MyClass("Buster"));
   mpets.insert(mpets.end(), gcnew MyClass("Zipper"));
   mpets.insert(gcnew MyClass("N"));
   multiset<MyClass^>::iterator New_Puppy = mpets.insert(gcnew MyClass("N"));
   for each (MyClass^ pet in mpets) 
       System::Console::Write("{0} ", pet->Name); 
   return (0);

}

 </source>


Get key and value Enumerator

<source lang="csharp">

  1. include "stdafx.h"
  2. using <system.dll>

using namespace System; using namespace System::Collections; using namespace System::Collections::Specialized; void main() {

   ListDictionary ^ldict = gcnew ListDictionary();
   ldict->Add("A", "First");
   ldict->Add("B", "Second");
   ldict->Add("C", "Third");
   ldict["D"] = "Fourth";
   
   IEnumerator ^keys = ldict->Keys->GetEnumerator();
   IEnumerator ^vals = ldict->Values->GetEnumerator();
   while ( keys->MoveNext() && vals->MoveNext())
   {
       Console::WriteLine("{0}\t\t{1}", keys->Current, vals->Current);
   }

}

 </source>


Push pets onto stack and display by IEnumerator<> interface

<source lang="csharp"> #include "stdafx.h"

  1. include <cliext/stack>

using namespace System; using namespace cliext; using namespace System::Collections::Generic; ref class MyClass { public:

   String^ Name;
   MyClass() : Name(String::Empty) { }
   MyClass(String^ name) : Name(name) { }
   MyClass(const MyClass% orig){
       Name = orig.Name; 
   }
   MyClass% operator=(const MyClass% orig){
       if (this != %orig)
           Name = orig.Name;
       return *this;
   }
   ~MyClass() { }
   bool operator<(const MyClass^ rhs){
       return (Name->CompareTo(rhs->Name) < 0);
   }
   bool operator==(const MyClass^ rhs){
       return (Name->Equals(rhs->Name));
   }

}; int main(array<System::String ^> ^args){

   stack<MyClass^> pets;
   pets.push(gcnew MyClass("A"));
   pets.push(gcnew MyClass("B"));
   pets.push(gcnew MyClass("C"));
   pets.push(gcnew MyClass("D"));
   for each (MyClass^ pet in pets.get_container())
       Console::Write("{0} ", pet->Name);
   return 0;

}

 </source>


Use while loop and Enumerator to loop through an array

<source lang="csharp">

  1. include "stdafx.h"

using namespace System; using namespace System::Collections; void main() {

   array<int>^ IntList = gcnew array<int> { 1, 2, 3, 4, 5 };
   IEnumerable ^collection = (IEnumerable^)IntList;
   IEnumerator ^enumerator = collection->GetEnumerator();
   while (enumerator->MoveNext())
   {
       int i = (int)enumerator->Current;
       Console::WriteLine(i);
   }
   for each (int i in IntList)
       Console::WriteLine(i);

}

 </source>


Using IEnumerator to loop through an array

<source lang="csharp">

  1. include "stdafx.h"

using namespace System; using namespace System::Collections; int main() {

  array<DateTime^>^ dateArray = gcnew array<DateTime^>(2);
  dateArray[0] = gcnew DateTime(1970, 12, 18);
  dateArray[1] = gcnew DateTime(1990, 1, 5);
  IEnumerator^ enumerator1 = dateArray->GetEnumerator();
  while ( enumerator1->MoveNext() )
  {
     DateTime^ current = (DateTime^) enumerator1->Current;
     Console::WriteLine( current->ToString("MM/dd/yyyy") );
  }

}

 </source>


vector for each loop using built in IEnumerator<> interface

<source lang="csharp">

  1. include "stdafx.h"
  2. include <cliext/vector>
  3. include <cliext/adapter>

using namespace System; using namespace cliext; using namespace System::Collections::Generic; ref class MyClass{ public:

   String^ Name;
   MyClass() : Name(String::Empty) { }
   MyClass(String^ name) : Name(name) { }
   MyClass(const MyClass% orig){
       Name = orig.Name; 
   }
   MyClass% operator=(const MyClass% orig){
       if (this != %orig)
           Name = orig.Name;
       return *this;
   }
   ~MyClass() { }
   bool operator<(const MyClass^ rhs){
       return (Name->CompareTo(rhs->Name) < 0);
   }
   bool operator==(const MyClass^ rhs){
       return (Name->Equals(rhs->Name));
   }

};

int main(array<System::String ^> ^args) {

   vector<MyClass^> pets; 
   pets.push_back(gcnew MyClass("A")); 
   pets.push_back(gcnew MyClass("B"));
   pets.push_back(gcnew MyClass("C"));
   pets.push_back(gcnew MyClass("D"));
   for each (MyClass^ pet in pets) 
       System::Console::Write("{0} ", pet->Name); 
   return (0); 

}

 </source>