An array of C + +/CLI managed + + introduces "4"

Source: Internet
Author: User
Tags array definition

Managed arrays are actually objects of type System::Array , so managed arrays created in a C + +/CLI program can use the various methods and properties provided by the class.


[1] Definition


[Qualifiers] [CLI::] array<[qualifiers]type[,dimension]> ^var;


Qualifiers

Storage method Description (optional). Selectable storage options include:mutable,volatile,const,extern , and STATC.


Array

The keyword for the managed array definition, which is defined in the CLI name domain. The using namespace CLI in a managed program project; is implied, so the name field of the array indicates the CLI:: Can be omitted


Type

The type name of the managed array element. The types that can be selected include the handle of the type ^ name, the value type name, or the local pointer name (for example, the value type's * pointer name).


Dimension

The number of dimensions of the managed array. The default dimension is 1 and the maximum number of dimensions is 32.


Var

The managed array name.


For example:

array<string^> ^ strArray1; One-dimensional string^ managed array

array<double, 2> ^ doublearray; Two-dimensional double-managed array

[2] Creating


var= gcnew [CLI::] array<[qualifiers]type[, Dimension]> (val[, Val ...]);


gcnew The managed object creation operator.

Val managed array specifies dimensions of dimensions


For example:

StrArray1 = gcnew array<string^> (50);

Doublearray = gcnew array<double, 2> (10, 10);


The creation of managed arrays can also be implemented using class System::Array , which provides methods CreateInstance to create managed array instances, for example: array<int> ^intarray =array: : CreateInstance (int, 100);


[3] Initialization


array<string^, 2>strarray =gcnew array<string^, 2>{

{L "John", L "male", L "New York"},

{L "Merry", L "female", L "Washington"}};

[3] Access


Access to managed arrays can be through subscript operators, or by using the GetValue and SetValue methods of class System::Array


int value = Intarray[5];

String ^str = strarray[2, 0];

String ^str = strarray->getvalue (2, 0);

Strarray->setvalue (L "Henrry", 2, 0);

[3] Sorting ( sorting method can only be used for one-dimensional arrays )


Class System::Array defines multiple versions of the static method sort to meet the multiple ordering needs of an array, for example, to sort all built-in numeric elements by default comparison algorithm (numeric ascending), Sort some built-in numeric elements by default comparison algorithm (numeric ascending), sort all built-in numeric elements in descending order, sort all custom elements by custom comparison algorithm, etc.


For example:

Intarray all elements are sorted by ascending algorithm:

Array::sort (Intarray);


Intarray the elements in the lower order 1 through 5 are sorted by ascending algorithm:

Array::sort (Intarray, 1, 5);

[4] Lookup ( Find method can only be used for one-dimensional arrays )


Class System::Array provides multiple versions of a static method BinarySearch to satisfy multiple query needs for an array, such as querying a specified element in all built-in numeric elements, Queries the specified element in a partially built-in numeric element, and queries the specified element by a custom algorithm in all custom type elements.


For example:

The statement that queries the specified element in all elements of the Intarray:

int pos = Array::binarysearch (Intarray, (object^) 25);

Returns the subscript position of the element in the array if the element being queried exists, otherwise returns-1.

An array of C + +/CLI managed + + introduces "4"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.