Visual C++ .NET/Collections/sort

Материал из .Net Framework эксперт
Версия от 15:05, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Sort a list

<source lang="csharp">

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

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->CompareTo(rhs->Name) > 0);
   }
   bool operator==(const MyClass^ rhs){
       return (Name->Equals(rhs->Name));
   }

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

   list<MyClass^> pets; 
   pets.push_front(gcnew MyClass("A")); 
   pets.push_front(gcnew MyClass("B"));
   pets.push_front(gcnew MyClass("C"));
   pets.push_front(gcnew MyClass("D"));
   ICollection<MyClass^>^ genericIList = %pets;
   genericIList->Add(gcnew MyClass("E"));
 pets.sort();
   for each (MyClass^ pet in genericIList) 
       System::Console::Write("{0} ", pet->Name); 
   return (0);

}

 </source>


sort and functor

<source lang="csharp">

  1. include "stdafx.h"
  2. include <cliext/vector>
  3. include <cliext/functional>
  4. include <cliext/algorithm>

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->CompareTo(rhs->Name) > 0);
   }
   bool operator==(const MyClass^ rhs)
   {
       return (Name->Equals(rhs->Name));
   }

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

   vector<MyClass^> myCollection; 
   myCollection.push_back(gcnew MyClass("A")); 
   myCollection.push_back(gcnew MyClass("B"));
   myCollection.push_back(gcnew MyClass("C"));
   myCollection.push_back(gcnew MyClass("D"));
   for each (MyClass^ pet in myCollection) 
       Console::Write("{0} ", pet->Name);
   Console::Write("\n\nsort(F,L, functor):\n   ");
   sort(myCollection.begin(), myCollection.end(), greater<MyClass^>());
   for each (MyClass^ pet in myCollection) 
       Console::Write("{0} ", pet->Name);
   return 0;

}

 </source>


Sort list by a user-defined functor

<source lang="csharp">

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

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->CompareTo(rhs->Name) > 0);
   }
   bool operator==(const MyClass^ rhs){
       return (Name->Equals(rhs->Name));
   }

}; ref class MyGreaterThanFunctor{ public:

   bool operator()(MyClass^ a, MyClass^ b)
   {
       return a > b;
   }

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

   list<MyClass^> pets; 
   pets.push_front(gcnew MyClass("A")); 
   pets.push_front(gcnew MyClass("B"));
   pets.push_front(gcnew MyClass("C"));
   pets.push_front(gcnew MyClass("D"));
   list<MyClass^> puppies; 
   puppies.push_front(gcnew MyClass("E")); 
   puppies.push_front(gcnew MyClass("F"));
 pets.sort(gcnew MyGreaterThanFunctor());
 puppies.sort(greater<MyClass^>());
   pets.merge(puppies, greater<MyClass^>());
   for each (MyClass^ pet in pets) 
       System::Console::Write("{0} ", pet->Name); 
   return (0);

}

 </source>


Sort list by greater-functor

<source lang="csharp">

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

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->CompareTo(rhs->Name) > 0);
   }
   bool operator==(const MyClass^ rhs){
       return (Name->Equals(rhs->Name));
   }

}; ref class MyGreaterThanFunctor{ public:

   bool operator()(MyClass^ a, MyClass^ b)
   {
       return a > b;
   }

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

   list<MyClass^> pets; 
   pets.push_front(gcnew MyClass("A")); 
   pets.push_front(gcnew MyClass("B"));
   pets.push_front(gcnew MyClass("C"));
   pets.push_front(gcnew MyClass("D"));
   list<MyClass^> puppies; 
   puppies.push_front(gcnew MyClass("E")); 
   puppies.push_front(gcnew MyClass("F"));
 pets.sort(gcnew MyGreaterThanFunctor());
 puppies.sort(greater<MyClass^>());
   pets.merge(puppies, greater<MyClass^>());
   for each (MyClass^ pet in pets) 
       System::Console::Write("{0} ", pet->Name); 
   return (0);

}

 </source>