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

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

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

Adding one list to another list

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections::Generic;
ref class MyClass{
public:
    String^ Value;
    MyClass(String^ in);
    virtual String^ ToString() override;
    static bool hasX(MyClass^ val);
    static void formatString(MyClass^ val);
};
MyClass::MyClass(String^ in) : Value(in) {}
String^ MyClass::ToString() { return Value; }
bool MyClass::hasX(MyClass^ val)
{
    return val->Value->ToUpper()->IndexOf("X") > 0;
}
void MyClass::formatString(MyClass^ val)
{
    val->Value = String::Format("** {0} **", val->Value);
}
void main(){
    List<MyClass^>^ alist = gcnew List<MyClass^>();
    alist->Add(gcnew MyClass("One"));
    alist->Add(gcnew MyClass("-"));
    alist[1] = gcnew MyClass("Three");
    alist->Insert(1, gcnew MyClass("Two"));
    List<MyClass^>^ morenums = gcnew List<MyClass^>();
    morenums->Add(gcnew MyClass("Four"));
    morenums->Add(gcnew MyClass("Five"));
 
    alist->AddRange(morenums);
}


Add to list with push_front

 
#include "stdafx.h"
#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"));

    return (0);
}


Add your class to a generic List

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections::Generic;
ref class MyClass{
public:
    String^ Value;
    MyClass(String^ in);
    virtual String^ ToString() override;
    static bool hasX(MyClass^ val);
    static void formatString(MyClass^ val);
};
MyClass::MyClass(String^ in) : Value(in) {}
String^ MyClass::ToString() { return Value; }
bool MyClass::hasX(MyClass^ val)
{
    return val->Value->ToUpper()->IndexOf("X") > 0;
}
void MyClass::formatString(MyClass^ val)
{
    val->Value = String::Format("** {0} **", val->Value);
}
void main(){
    List<MyClass^>^ alist = gcnew List<MyClass^>();
    alist->Add(gcnew MyClass("One"));
    alist->Add(gcnew MyClass("-"));
    alist[1] = gcnew MyClass("Three");

}


For each loop and list

 
#include "stdafx.h"
#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);
}


For loop and element count

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections::Generic;
ref class MyClass{
public:
    String^ Value;
    MyClass(String^ in);
    virtual String^ ToString() override;
    static bool hasX(MyClass^ val);
    static void formatString(MyClass^ val);
};
MyClass::MyClass(String^ in) : Value(in) {}
String^ MyClass::ToString() { return Value; }
bool MyClass::hasX(MyClass^ val)
{
    return val->Value->ToUpper()->IndexOf("X") > 0;
}
void MyClass::formatString(MyClass^ val)
{
    val->Value = String::Format("** {0} **", val->Value);
}
void main(){
    List<MyClass^>^ alist = gcnew List<MyClass^>();
    alist->Add(gcnew MyClass("One"));
    alist->Add(gcnew MyClass("-"));
    alist[1] = gcnew MyClass("Three");
    for (int i = 0; i < alist->Count; i++)
        Console::WriteLine("{0} ", alist[i]);
}


Get the first element in a list

 
#include "stdafx.h"
#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"));

    list<MyClass^>::iterator pet_i = pets.begin();   
    pets.insert(++pet_i, gcnew MyClass("Jack"));
    for(pet_i = pets.begin(); pet_i != pets.end(); pet_i++)
        System::Console::Write("{0} ", pet_i->Name); 
    return (0);
}


Insert element to a list

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections::Generic;
ref class MyClass{
public:
    String^ Value;
    MyClass(String^ in);
    virtual String^ ToString() override;
    static bool hasX(MyClass^ val);
    static void formatString(MyClass^ val);
};
MyClass::MyClass(String^ in) : Value(in) {}
String^ MyClass::ToString() { return Value; }
bool MyClass::hasX(MyClass^ val)
{
    return val->Value->ToUpper()->IndexOf("X") > 0;
}
void MyClass::formatString(MyClass^ val)
{
    val->Value = String::Format("** {0} **", val->Value);
}
void main(){
    List<MyClass^>^ alist = gcnew List<MyClass^>();
    alist->Add(gcnew MyClass("One"));
    alist->Add(gcnew MyClass("-"));
    alist[1] = gcnew MyClass("Three");
    alist->Insert(1, gcnew MyClass("Two"));
}


Insert from .NET generic List<T>

 
#include "stdafx.h"
#include <cliext/set>
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)
{
    set<MyClass^> pets; 
    MyClass^ King = gcnew MyClass("K");
    pets.insert(pets.end(), gcnew MyClass("A"));
    pets.insert(King); 
    pets.insert(gcnew MyClass("B"));
    
    List<MyClass^>^ morepets = gcnew List<MyClass^>();
    morepets->Add(gcnew MyClass("D"));
    morepets->Add(gcnew MyClass("AA"));
    pets.insert(morepets);
    for each (MyClass^ pet in pets) 
        System::Console::Write("{0} ", pet->Name); 
    return (0);
}


Insert to a list

 
#include "stdafx.h"
#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"));

    list<MyClass^>::iterator pet_i = pets.begin();   
    pets.insert(++pet_i, gcnew MyClass("Jack"));
    for(pet_i = pets.begin(); pet_i != pets.end(); pet_i++)
        System::Console::Write("{0} ", pet_i->Name); 
    return (0);
}


List.FindAll with predicate

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections::Generic;
ref class MyClass{
public:
    String^ Value;
    MyClass(String^ in);
    virtual String^ ToString() override;
    static bool hasX(MyClass^ val);
    static void formatString(MyClass^ val);
};
MyClass::MyClass(String^ in) : Value(in) {}
String^ MyClass::ToString() { return Value; }
bool MyClass::hasX(MyClass^ val)
{
    return val->Value->ToUpper()->IndexOf("X") > 0;
}
void MyClass::formatString(MyClass^ val)
{
    val->Value = String::Format("** {0} **", val->Value);
}
void main(){
    List<MyClass^>^ alist = gcnew List<MyClass^>();
    alist->Add(gcnew MyClass("One"));
    alist->Add(gcnew MyClass("-"));
    alist[1] = gcnew MyClass("Three");
    for (int i = 0; i < alist->Count; i++)
        Console::WriteLine("{0} ", alist[i]);
    List<MyClass^>^ x = alist->FindAll(gcnew Predicate<MyClass^>(MyClass::hasX));
}


List.ForEach and Action

 
#include "stdafx.h"
using namespace System;
using namespace System::Collections::Generic;
ref class MyClass{
public:
    String^ Value;
    MyClass(String^ in);
    virtual String^ ToString() override;
    static bool hasX(MyClass^ val);
    static void formatString(MyClass^ val);
};
MyClass::MyClass(String^ in) : Value(in) {}
String^ MyClass::ToString() { return Value; }
bool MyClass::hasX(MyClass^ val)
{
    return val->Value->ToUpper()->IndexOf("X") > 0;
}
void MyClass::formatString(MyClass^ val)
{
    val->Value = String::Format("** {0} **", val->Value);
}
void main(){
    List<MyClass^>^ alist = gcnew List<MyClass^>();
    alist->Add(gcnew MyClass("One"));
    alist->Add(gcnew MyClass("-"));
    alist[1] = gcnew MyClass("Three");
    for (int i = 0; i < alist->Count; i++)
        Console::WriteLine("{0} ", alist[i]);
        
    alist->ForEach(gcnew Action<MyClass^>(MyClass::formatString));

    for each(MyClass^ str in alist)
        Console::WriteLine("{0} ", str);
}


Loop through a list with for loop

 
#include "stdafx.h"
#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"));

    list<MyClass^>::iterator pet_i = pets.begin();   
    pets.insert(++pet_i, gcnew MyClass("Jack"));
    for(pet_i = pets.begin(); pet_i != pets.end(); pet_i++)
        System::Console::Write("{0} ", pet_i->Name); 
    return (0);
}


Merge lists by greater-functor

 
#include "stdafx.h"
#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);
}


Pointer for list element

 
#include "stdafx.h"
#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"));
    list<MyClass^> myList; 
    list<MyClass^>::iterator pet_ir = pets.begin();
    int randMyClassNo = (gcnew Random())->Next(pets.size());
    for (int i = 0; i < randMyClassNo; i++)
        pet_ir++;
    myList.splice(myList.begin(), pets, pet_ir);
    for each (MyClass^ pet in pets) 
        System::Console::Write("{0} ", pet->Name); 
    for each (MyClass^ pet in myList) 
        System::Console::Write("{0} ", pet->Name); 
    return (0);
}


Splice a list

 
#include "stdafx.h"
#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"));
    list<MyClass^> myList; 
    list<MyClass^>::iterator pet_ir = pets.begin();
    int randMyClassNo = (gcnew Random())->Next(pets.size());
    for (int i = 0; i < randMyClassNo; i++)
        pet_ir++;
    myList.splice(myList.begin(), pets, pet_ir);
    for each (MyClass^ pet in pets) 
        System::Console::Write("{0} ", pet->Name); 
    for each (MyClass^ pet in myList) 
        System::Console::Write("{0} ", pet->Name); 
    return (0);
}