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

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

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

Add a range to StringCollection

 
#include "stdafx.h"
#using <system.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void main()
{
    StringCollection ^strcol = gcnew StringCollection();
    array<String^>^ tmpstr = gcnew array<String^> {"Third", "Fourth" };
    strcol->AddRange(tmpstr);
}


Add keys from a StringDictionary to an ArrayList

 
#include "stdafx.h"
#using <system.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void main()
{
    StringDictionary ^strdict = gcnew StringDictionary();
    strdict->Add("D", "d");
    strdict->Add("F", "f");
    strdict["C"] = "c";
    
    ArrayList ^alist = gcnew ArrayList();
    alist->AddRange(strdict->Keys);
    alist->Sort();
}


Add key value pair to StringDictionary

 
#include "stdafx.h"
#using <system.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void main()
{
    StringDictionary ^strdict = gcnew StringDictionary();
    strdict->Add("D", "d");
    strdict->Add("F", "f");
}


Add string to StringCollection

 
#include "stdafx.h"
#using <system.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void main()
{
    StringCollection ^strcol = gcnew StringCollection();
    strcol->Add("The first String");
}


Assign string value to StringCollection by indexer

 
#include "stdafx.h"
#using <system.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void main()
{
    StringCollection ^strcol = gcnew StringCollection();
    array<String^>^ tmpstr = gcnew array<String^> {"Third", "Fourth" };
    strcol->AddRange(tmpstr);
    strcol[0] = "First";
    strcol->Insert(1, "Second");
}


Assign value to a StringDictionary by using indexer

 
#include "stdafx.h"
#using <system.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void main()
{
    StringDictionary ^strdict = gcnew StringDictionary();
    strdict->Add("D", "d");
    strdict->Add("F", "f");
    strdict["C"] = "c";
}


Get all keys from a StringDictionary

 
#include "stdafx.h"
#using <system.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void main()
{
    StringDictionary ^strdict = gcnew StringDictionary();
    strdict->Add("D", "d");
    strdict->Add("F", "f");
    strdict["C"] = "c";
    
    ArrayList ^alist = gcnew ArrayList();
    alist->AddRange(strdict->Keys);
    alist->Sort();
}


Get Enumerator from StringCollection

 
#include "stdafx.h"
#using <system.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void main()
{
    StringCollection ^strcol = gcnew StringCollection();
    array<String^>^ tmpstr = gcnew array<String^> {"Third", "Fourth" };
    strcol->AddRange(tmpstr);
    StringEnumerator ^strenum = strcol->GetEnumerator();
    while ( strenum->MoveNext())
    {
        Console::WriteLine(strenum->Current);
    }
}


Get value from StringDictionary by key

 
#include "stdafx.h"
#using <system.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void main()
{
    StringDictionary ^strdict = gcnew StringDictionary();
    strdict->Add("D", "d");
    strdict->Add("F", "f");
    strdict["C"] = "c";
    
    ArrayList ^alist = gcnew ArrayList();
    alist->AddRange(strdict->Keys);
    alist->Sort();
    
    for (int i = 0; i < alist->Count; i++)
    {
        Console::WriteLine("{0,10}:\t{1}", alist[i], 
            strdict[(String^)alist[i]]);
    }
}


Insert string to a StringCollection at specific position

 

#include "stdafx.h"
#using <system.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void main()
{
    StringCollection ^strcol = gcnew StringCollection();
    array<String^>^ tmpstr = gcnew array<String^> {"Third", "Fourth" };
    strcol->AddRange(tmpstr);
    strcol->Insert(1, "Second");
}


Loop through a StringDictionary with for loop

 
#include "stdafx.h"
#using <system.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void main()
{
    StringDictionary ^strdict = gcnew StringDictionary();
    strdict->Add("D", "d");
    strdict->Add("F", "f");
    strdict["C"] = "c";
    
    ArrayList ^alist = gcnew ArrayList();
    alist->AddRange(strdict->Keys);
    alist->Sort();
    
    for (int i = 0; i < alist->Count; i++)
    {
        Console::WriteLine("{0,10}:\t{1}", alist[i], 
            strdict[(String^)alist[i]]);
    }
}


Using for each loop with StringCollection

 
#include "stdafx.h"
#using <system.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void main()
{
    StringCollection ^strcol = gcnew StringCollection();
    array<String^>^ tmpstr = gcnew array<String^> {"Third", "Fourth" };
    strcol->AddRange(tmpstr);

    for each (String^ s in strcol)
        Console::WriteLine(s);
}