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

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

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

Create map between Class and double value

 
#include "stdafx.h"
#include <cliext/map>
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)
{
    map<MyClass^, double> myMap; 
    MyClass^ King = gcnew MyClass("A"); 
    myMap.insert(myMap.end(), map<MyClass^, double>::make_value(gcnew MyClass("Z"), 3.0));
    myMap.insert(map<MyClass^, double>::make_value(King, 10.0)); 
    myMap.insert(map<MyClass^, double>::make_value(gcnew MyClass("B"),9.0));
    return (0);
}


Erase element from a map

 
#include "stdafx.h"
#include <cliext/map>
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)
{
    map<MyClass^, double> myMap; 
    MyClass^ King = gcnew MyClass("A"); 
    myMap.insert(myMap.end(), map<MyClass^, double>::make_value(gcnew MyClass("Z"), 3.0));
    myMap.insert(map<MyClass^, double>::make_value(King, 10.0)); 
    myMap.insert(map<MyClass^, double>::make_value(gcnew MyClass("B"),9.0));
    map<MyClass^, double> copy;
    map<MyClass^, double>::iterator myMap_e = myMap.end();
    copy.insert(myMap.begin(), --myMap_e);
    map<MyClass^, double>::iterator myMap_cb = copy.begin();
    copy.erase(++myMap_cb);
    copy.erase(gcnew MyClass("Lady"));
    for each (KeyValuePair<MyClass^, double> ^kvp in (IDictionary<MyClass^, double>^)%copy)
        System::Console::Write("{0} [{1}]  ", kvp->Key->Name, kvp->Value);
        
    return (0);
}


Find a key-value pair in a map

 
#include "stdafx.h"
#include <cliext/map>
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)
{
    map<MyClass^, double> myMap; 
    MyClass^ King = gcnew MyClass("A"); 
    myMap.insert(myMap.end(), map<MyClass^, double>::make_value(gcnew MyClass("Z"), 3.0));
    myMap.insert(map<MyClass^, double>::make_value(King, 10.0)); 
    myMap.insert(map<MyClass^, double>::make_value(gcnew MyClass("B"),9.0));
    map<MyClass^, double> copy;
    map<MyClass^, double>::iterator myMap_e = myMap.end();
    copy.insert(myMap.begin(), --myMap_e);
    map<MyClass^, double>::iterator myMap_cb = copy.begin();
    copy.erase(++myMap_cb);
    copy.erase(gcnew MyClass("Lady"));
    for each (KeyValuePair<MyClass^, double> ^kvp in (IDictionary<MyClass^, double>^)%copy)
        System::Console::Write("{0} [{1}]  ", kvp->Key->Name, kvp->Value);

    Console::WriteLine("\n\nFound = {0}", copy.find(King)->first->Name);
    map<MyClass^, double>::iterator Lady = copy.find(gcnew MyClass("Lady"));
    Console::WriteLine("Did {0}Find = Lady", (Lady == copy.end()) ? "not " : "");
            
    return (0);
}


Insert element to a map

 
#include "stdafx.h"
#include <cliext/map>
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)
{
    map<MyClass^, double> myMap; 
    MyClass^ King = gcnew MyClass("A"); 
    myMap.insert(myMap.end(), map<MyClass^, double>::make_value(gcnew MyClass("Z"), 3.0));
    myMap.insert(map<MyClass^, double>::make_value(King, 10.0)); 
    myMap.insert(map<MyClass^, double>::make_value(gcnew MyClass("B"),9.0));
    map<MyClass^, double> copy;
    map<MyClass^, double>::iterator myMap_e = myMap.end();
    copy.insert(myMap.begin(), --myMap_e);
    map<MyClass^, double>::iterator myMap_cb = copy.begin();
    copy.erase(++myMap_cb);
    copy.erase(gcnew MyClass("Lady"));
    for each (KeyValuePair<MyClass^, double> ^kvp in (IDictionary<MyClass^, double>^)%copy)
        System::Console::Write("{0} [{1}]  ", kvp->Key->Name, kvp->Value);
        
    return (0);
}


Insert myMap from .NET Dictionary<T>

 
#include "stdafx.h"
#include <cliext/map>
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)
{
    map<MyClass^, double> myMap; 
    MyClass^ King = gcnew MyClass("A"); 
    myMap.insert(myMap.end(), map<MyClass^, double>::make_value(gcnew MyClass("Z"), 3.0));
    myMap.insert(map<MyClass^, double>::make_value(King, 10.0)); 
    myMap.insert(map<MyClass^, double>::make_value(gcnew MyClass("B"),9.0));
    Dictionary<MyClass^, double>^ moremyMap = gcnew Dictionary<MyClass^, double>();
    moremyMap->Add(gcnew MyClass("Daisy"), 5);
    moremyMap->Add(gcnew MyClass("Lady"), 7);
    for each (KeyValuePair<MyClass^, double> ^kvp in moremyMap)
        myMap.insert(map<MyClass^, double>::make_value(kvp->Key, kvp->Value));
        
    return (0);
}


Insert to a map

 
#include "stdafx.h"
#include <cliext/map>
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)
{
    map<MyClass^, double> myMap; 
    MyClass^ King = gcnew MyClass("A"); 
    myMap.insert(myMap.end(), map<MyClass^, double>::make_value(gcnew MyClass("Z"), 3.0));
    myMap.insert(map<MyClass^, double>::make_value(King, 10.0)); 
    myMap.insert(map<MyClass^, double>::make_value(gcnew MyClass("B"),9.0));
    return (0);
}


Is insert operation successful

 
#include "stdafx.h"
#include <cliext/map>
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)
{
    map<MyClass^, double> myMap; 
    MyClass^ King = gcnew MyClass("A"); 
    myMap.insert(myMap.end(), map<MyClass^, double>::make_value(gcnew MyClass("Z"), 3.0));
    myMap.insert(map<MyClass^, double>::make_value(King, 10.0)); 
    myMap.insert(map<MyClass^, double>::make_value(gcnew MyClass("B"),9.0));
    map<MyClass^, double>::pair_iter_bool success;
    success = myMap.insert(map<MyClass^, double>::make_value(gcnew MyClass("N"),0.1));
    Console::WriteLine("First Time {0} age {1} is added {2}", 
        success.first->first->Name, 
        success.first->second, 
        success.second ? "successfully" : "unsuccessfully");
    return (0);
}


List of a Map

 
#include "stdafx.h"
#include <cliext/map>
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)
{
    map<MyClass^, double> myMap; 
    MyClass^ King = gcnew MyClass("A"); 
    myMap.insert(myMap.end(), map<MyClass^, double>::make_value(gcnew MyClass("Z"), 3.0));
    myMap.insert(map<MyClass^, double>::make_value(King, 10.0)); 
    myMap.insert(map<MyClass^, double>::make_value(gcnew MyClass("B"),9.0));
    List<map<MyClass^, double>::value_type>^ evenmoremyMap = gcnew List<map<MyClass^, double>::value_type>();
    evenmoremyMap->Add(map<MyClass^, double>::make_value(gcnew MyClass("AA"), 1));
    evenmoremyMap->Add(map<MyClass^, double>::make_value(gcnew MyClass("AAA"), 2));
    myMap.insert(evenmoremyMap);
    IDictionary<MyClass^, double>^ myMap_IDict = %myMap;
    for each (KeyValuePair<MyClass^, double> ^kvp in myMap_IDict)
        System::Console::Write("{0} [{1}]  ", kvp->Key->Name, kvp->Value);
        
    return (0);
}


Loop through map with IDictionary<> interface

 
#include "stdafx.h"
#include <cliext/map>
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)
{
    map<MyClass^, double> myMap; 
    MyClass^ King = gcnew MyClass("A"); 
    myMap.insert(myMap.end(), map<MyClass^, double>::make_value(gcnew MyClass("Z"), 3.0));
    myMap.insert(map<MyClass^, double>::make_value(King, 10.0)); 
    myMap.insert(map<MyClass^, double>::make_value(gcnew MyClass("B"),9.0));
    Dictionary<MyClass^, double>^ moremyMap = gcnew Dictionary<MyClass^, double>();
    moremyMap->Add(gcnew MyClass("Daisy"), 5);
    moremyMap->Add(gcnew MyClass("Lady"), 7);
    for each (KeyValuePair<MyClass^, double> ^kvp in moremyMap)
        myMap.insert(map<MyClass^, double>::make_value(kvp->Key, kvp->Value));
        
    return (0);
}


Map reverse interator

 
#include "stdafx.h"
#include <cliext/map>
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)
{
    map<MyClass^, double> myMap; 
    MyClass^ King = gcnew MyClass("A"); 
    myMap.insert(myMap.end(), map<MyClass^, double>::make_value(gcnew MyClass("Z"), 3.0));
    myMap.insert(map<MyClass^, double>::make_value(King, 10.0)); 
    myMap.insert(map<MyClass^, double>::make_value(gcnew MyClass("B"),9.0));
    map<MyClass^, double>::pair_iter_bool success;
    success = myMap.insert(map<MyClass^, double>::make_value(gcnew MyClass("N"),0.1));
    Console::WriteLine("First Time {0} age {1} is added {2}", 
        success.first->first->Name, 
        success.first->second, 
        success.second ? "successfully" : "unsuccessfully");
    map<MyClass^, double>::reverse_iterator pet_ri;
    for(pet_ri = myMap.rbegin(); pet_ri != myMap.rend(); pet_ri++)
        System::Console::Write("{0} [{1}]  ", pet_ri->first->Name, pet_ri->second); 
        
    return (0);
}