Visual C++ .NET/Collections/Stack — различия между версиями

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

Текущая версия на 12:05, 26 мая 2010

Call Pop method to remove element from a Stack

 
#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();
}


Get element count for a Stack

 
#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();
}


Pop pets from stack top till empty

 
#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"));

    while (!pets.empty())
    {
        Console::Write("{0} ", pets.top()->Name);
        pets.pop();
    }
    return 0;
}


Push value to a Stack

 
#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();
}


Push value to stack

 
#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"));

    return 0;
}