Visual C++ .NET/Collections/Vector — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:05, 26 мая 2010
Содержание
- 1 Insert value to a vector
- 2 Insert value to a vector with Add()
- 3 pop_back() from a vector
- 4 Push class into vector
- 5 Push value to vector
- 6 Reference the start and end of a vector
- 7 Reverse a vector
- 8 Sort a vector
- 9 Using for each to loop through a vector
- 10 Vector for loop -- Using const_iterator
- 11 Vector for loop -- Using subscript
Insert value to a vector
#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"));
pets.insert(pets.begin() + 1, gcnew MyClass("J"));
vector<MyClass^>::const_iterator pet_i;
for(pet_i = pets.begin(); pet_i != pets.end(); pet_i++)
System::Console::Write("{0} ", pet_i->Name);
return (0);
}
Insert value to a vector with Add()
#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"));
IList<MyClass^>^ genericIList = %pets;
genericIList->Add(gcnew MyClass("Queen"));
for each (MyClass^ pet in genericIList)
System::Console::Write("{0} ", pet->Name);
return (0);
}
pop_back() from a vector
#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"));
vector<MyClass^>^ vMyClass = gcnew vector<MyClass^>();
vMyClass->pop_back();
vector<MyClass^>::reverse_iterator pet_ri;
for(pet_ri = vMyClass->rbegin(); pet_ri != vMyClass->rend(); pet_ri++)
System::Console::Write("{0} ", pet_ri->Name);
return (0);
}
Push class into vector
#include "stdafx.h"
#include <cliext/vector>
#include <cliext/functional>
#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\nreverse(F,L):\n ");
reverse(myCollection.begin(), myCollection.end());
for each (MyClass^ pet in myCollection)
Console::Write("{0} ", pet->Name);
return 0;
}
Push value to vector
#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"));
return (0);
}
Reference the start and end of a vector
#include "stdafx.h"
#include <cliext/vector>
#include <cliext/functional>
#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):\n ");
sort(myCollection.begin(), myCollection.end());
for each (MyClass^ pet in myCollection)
Console::Write("{0} ", pet->Name);
return 0;
}
Reverse a vector
#include "stdafx.h"
#include <cliext/vector>
#include <cliext/functional>
#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\nreverse(F,L):\n ");
reverse(myCollection.begin(), myCollection.end());
for each (MyClass^ pet in myCollection)
Console::Write("{0} ", pet->Name);
return 0;
}
Sort a vector
#include "stdafx.h"
#include <cliext/vector>
#include <cliext/functional>
#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):\n ");
sort(myCollection.begin(), myCollection.end());
for each (MyClass^ pet in myCollection)
Console::Write("{0} ", pet->Name);
return 0;
}
Using for each to loop through a vector
#include "stdafx.h"
#include <cliext/vector>
#include <cliext/functional>
#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\nreverse(F,L):\n ");
reverse(myCollection.begin(), myCollection.end());
for each (MyClass^ pet in myCollection)
Console::Write("{0} ", pet->Name);
return 0;
}
Vector for loop -- Using const_iterator
#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"));
pets.insert(pets.begin() + 1, gcnew MyClass("J"));
vector<MyClass^>::const_iterator pet_i;
for(pet_i = pets.begin(); pet_i != pets.end(); pet_i++)
System::Console::Write("{0} ", pet_i->Name);
return (0);
}
Vector for loop -- Using subscript
#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(int i=0; i < pets.size(); i++)
System::Console::Write("{0} ", pets[i]->Name);
return (0);
}