Visual C++ .NET/Collections/IEnumerator — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:04, 26 мая 2010
Содержание
- 1 Display elements in a multiset by IEnumerator<> interface
- 2 Get key and value Enumerator
- 3 Push pets onto stack and display by IEnumerator<> interface
- 4 Use while loop and Enumerator to loop through an array
- 5 Using IEnumerator to loop through an array
- 6 vector for each loop using built in IEnumerator<> interface
Display elements in a multiset by IEnumerator<> interface
#include "stdafx.h"
#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);
}
Get key and value Enumerator
#include "stdafx.h"
#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);
}
}
Push pets onto stack and display by IEnumerator<> interface
#include "stdafx.h"
#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;
}
Use while loop and Enumerator to loop through an array
#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);
}
Using IEnumerator to loop through an array
#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") );
}
}
vector for each loop using built in IEnumerator<> interface
#include "stdafx.h"
#include <cliext/vector>
#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);
}