Visual C++ .NET/Collections/Stack

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

Call Pop method to remove element from a Stack

<source lang="csharp">

  1. include "stdafx.h"

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

   Queue ^que = gcnew Queue();
   Stack ^stk = gcnew Stack();
   array<String^>^ entry = gcnew array<String^> { "First", "Second", "Third", "Fourth" };
   for (int i = 0; i < entry->Length; i++)
   {
       que->Enqueue(entry[i]);
       stk->Push(entry[i]);  
       Console::WriteLine("{0}\t\t{1}", entry[i], entry[i]);
   }
   while ((que->Count > 0) && (stk->Count > 0))
   {
       Console::WriteLine("{0}\t\t{1}", que->Dequeue(), stk->Pop());
   }
   que->Clear();
   stk->Clear();

}

 </source>


Get element count for a Stack

<source lang="csharp">

  1. include "stdafx.h"

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

   Queue ^que = gcnew Queue();
   Stack ^stk = gcnew Stack();
   array<String^>^ entry = gcnew array<String^> { "First", "Second", "Third", "Fourth" };
   for (int i = 0; i < entry->Length; i++)
   {
       que->Enqueue(entry[i]);
       stk->Push(entry[i]);  
       Console::WriteLine("{0}\t\t{1}", entry[i], entry[i]);
   }
   while ((que->Count > 0) && (stk->Count > 0))
   {
       Console::WriteLine("{0}\t\t{1}", que->Dequeue(), stk->Pop());
   }
   que->Clear();
   stk->Clear();

}

 </source>


Pop pets from stack top till empty

<source lang="csharp">

  1. include "stdafx.h"
  2. 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"));
   while (!pets.empty())
   {
       Console::Write("{0} ", pets.top()->Name);
       pets.pop();
   }
   return 0;

}

 </source>


Push value to a Stack

<source lang="csharp">

  1. include "stdafx.h"

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

   Queue ^que = gcnew Queue();
   Stack ^stk = gcnew Stack();
   array<String^>^ entry = gcnew array<String^> { "First", "Second", "Third", "Fourth" };
   for (int i = 0; i < entry->Length; i++)
   {
       que->Enqueue(entry[i]);
       stk->Push(entry[i]);  
       Console::WriteLine("{0}\t\t{1}", entry[i], entry[i]);
   }
   while ((que->Count > 0) && (stk->Count > 0))
   {
       Console::WriteLine("{0}\t\t{1}", que->Dequeue(), stk->Pop());
   }
   que->Clear();
   stk->Clear();

}

 </source>


Push value to stack

<source lang="csharp">

  1. include "stdafx.h"
  2. 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"));
   return 0;

}

 </source>