Sunday, October 18, 2009

visual c++ sorting array

The Array Class

Single-Dimensional Array Creation

An array is a technique of storing similar information of different items in a common variable of one name. Based on this, consider the following list:

Store Item
Women Coat
Men Jacket
Teen Jeans
Women Bathing Suit
Children Playground Dress

This type of list can be created using a String-based array. Here is an example:

System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e) {   String *lstItemsNames[] = { S"Women Coat", S"Men Jacket", S"Teen Jeans",                           S"Women Bathing Suit", S"Children Playground Dress" }; }

The above type of list is referred to as a single-dimensional. To provide more information about items, you can associate a second or even a third list to the first as follows:

System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e) {   String *itemsNumbers[] = { S"624376", S"274952", S"497852", S"749752", S"394085" };   String *lstItemsNames[]    = { S"Women Coat", S"Men Jacket", S"Teen Jeans",                                S"Women Bathing Suit", S"Children Playground Dress" };   double itemsPrices[]      = { 225.55, 175.75, 24.50, 34.65, 75.05 }; }

To support C++ arrays, the .NET Framework provides the Array class, which is defined in the System namespace. Based on this, you can formally use the Array class to create an array. To support the creation of an array, the Array class is equipped with the CreateInstance() method that comes in various versions. To create a one-dimensional array, you can use the following version:

public: static Array* CreateInstance(Type* elementType, int length);

The first argument is used to specify the type of array you want to create. Since it is declared as Type, you can use the __typeof operator to cast your type.

The second argument specifies the number of items of the list. Like a normal C++ array, the items in an Array-based list use a zero-based index, meaning that the first item has an index of 0, the second has an index of 1, and so on. Using the Array class, you can create a list of the above item names as follows:

System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e) {   Array *lstItemsNames = Array::CreateInstance(__typeof(String), length); }

Creating various lists that would be later connected can be confusing and even overwhelming, as mentioned earlier. http://www.functionx.com/vcnet/collections/arrays.htm

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=3714&lngWId=3

No comments:

Post a Comment