Visual C++ .NET/Collections/queue
Содержание
Add value to a Queue with Enqueue
#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();
}
Call dequeue to remove element from a Queue
#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();
}
Display Queue by IEnumerator<> interface
#include "stdafx.h"
#include <cliext/queue>
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)
{
queue<MyClass^> petq;
petq.push(gcnew MyClass("A"));
petq.push(gcnew MyClass("B"));
petq.push(gcnew MyClass("C"));
petq.push(gcnew MyClass("D"));
for each (MyClass^ pet in petq.get_container())
Console::Write("{0} ", pet->Name);
return 0;
}
Get element count for a Queue
#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 the front of a queue
#include "stdafx.h"
#include <cliext/queue>
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)
{
queue<MyClass^> petq;
petq.push(gcnew MyClass("A"));
petq.push(gcnew MyClass("B"));
petq.push(gcnew MyClass("C"));
petq.push(gcnew MyClass("D"));
while (!petq.empty())
{
Console::Write("{0} ", petq.front()->Name);
petq.pop();
}
return 0;
}
Get the last element in a queue
#include "stdafx.h"
#include <cliext/queue>
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)
{
queue<MyClass^> petq;
petq.push(gcnew MyClass("A"));
petq.push(gcnew MyClass("B"));
petq.push(gcnew MyClass("C"));
petq.push(gcnew MyClass("D"));
queue<MyClass^>::value_type lastpet = petq.back();
System::Console::WriteLine("The last pet in queue is:\n{0}", lastpet->Name);
return 0;
}
Is queue empty
#include "stdafx.h"
#include <cliext/queue>
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)
{
queue<MyClass^> petq;
petq.push(gcnew MyClass("A"));
petq.push(gcnew MyClass("B"));
petq.push(gcnew MyClass("C"));
petq.push(gcnew MyClass("D"));
while (!petq.empty())
{
Console::Write("{0} ", petq.front()->Name);
petq.pop();
}
return 0;
}
Push element to a queue
#include "stdafx.h"
#include <cliext/queue>
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)
{
queue<MyClass^> petq;
petq.push(gcnew MyClass("A"));
petq.push(gcnew MyClass("B"));
petq.push(gcnew MyClass("C"));
petq.push(gcnew MyClass("D"));
return 0;
}