Visual C++ .NET/Collections/Array — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:05, 26 мая 2010
Содержание
- 1 Array equality test
- 2 Array for each
- 3 Array initialization without gcnew
- 4 Array initializing
- 5 Arrays iterators
- 6 Assign value from native array to managed array
- 7 class with managed array
- 8 Create an interior pointer to elements of the array
- 9 Create an object for each object array position
- 10 Declare, create, and initialize a 1D managed array
- 11 Declare, create, and initialize a 1D native array
- 12 Declare, create, and initialize a 2D managed array
- 13 Declare, create, and initialize a 2D native array
- 14 Get array length
- 15 Loop over the array with the interior pointer
- 16 Object array with non default constrctuctor
- 17 Shallow copy creates another name for the array
- 18 Single dimension Arrays
- 19 Sort array with Array.Sort
- 20 String arrays length
- 21 Use variables in the initializer list
- 22 Using Array.BinarySearch to search an element in an array
- 23 Using Array::Copy to copy array
- 24 Using array.Equals to test if two arrays are equal
- 25 Using for each to loop through an array
- 26 Using gcnew to create array
Array equality test
#include "stdafx.h"
using namespace System;
bool ReallyEquals(array<int>^ a, array<int>^ b){
if (a->Length != b->Length)
return false;
for (int i = 0; i < a->Length; i++)
{
if (a[i] != b[i]) return false;
}
return true;
}
int main()
{
array<int>^ ai1 = gcnew array<int> { 1, 2 };
array<int>^ ai2 = gcnew array<int> { 1, 2 };
if ( ai1 == ai2 )
{
Console::WriteLine("The arrays are equal using the == operator.");
}
if (ai1->Equals(ai2) )
{
Console::WriteLine("The arrays are equal using the Equals method.");
}
if (ReallyEquals(ai1, ai2))
{
Console::WriteLine(
"The arrays are equal using element-by-element comparison.");
}
}
Array for each
#include "stdafx.h"
using namespace System;
int main()
{
array<String^>^ stringArray = gcnew array<String^>
{ "one", "two", "three", "four", "five" };
for each (String^ str in stringArray)
{
Console::WriteLine(str);
}
}
Array initialization without gcnew
#include "stdafx.h"
int main()
{
array<int>^ array_int1 = { 0, 1, 2 };
array<int>^ array_int2 = gcnew array<int> { 0, 1, 2 };
int i = 1, j = 2, k = 3;
array<int>^ array_int3 = { i, j, k };
}
Array initializing
#include "stdafx.h"
int main()
{
array<int>^ array_int1 = { 0, 1, 2 };
array<int>^ array_int2 = gcnew array<int> { 0, 1, 2 };
int i = 1, j = 2, k = 3;
array<int>^ array_int3 = { i, j, k };
}
Arrays iterators
#include "stdafx.h"
using namespace System;
using namespace System::Collections;
int main()
{
array<DateTime^>^ dateArray = gcnew array<DateTime^>(2);
dateArray[0] = gcnew DateTime(1970, 12, 18);
dateArray[1] = gcnew DateTime(1990, 1, 5);
IEnumerator^ enumerator1 = dateArray->GetEnumerator();
while ( enumerator1->MoveNext() )
{
DateTime^ current = (DateTime^) enumerator1->Current;
Console::WriteLine( current->ToString("MM/dd/yyyy") );
}
}
Assign value from native array to managed array
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int nativeArray[10];
array<int>^ managedArray = gcnew array<int>(10);
for (int i = 0; i < 10; i++)
{
nativeArray[i] = i*i;
cout << nativeArray[i] << " ";
managedArray[i] = nativeArray[i];
cout << managedArray[i] << " ";
}
cout << endl;
}
class with managed array
#include "stdafx.h"
ref class MyClass
{
private:
array<double>^ pos; // declare the managed array
unsigned int a;
unsigned int b;
public:
MyClass()
{
pos = gcnew array<double>(3);
pos[0] = 0; pos[1] = 0; pos[2] = 0;
a = 1;
b = 1;
}
MyClass(double x, double y, double z, unsigned int atNo, unsigned int n): a(atNo), b(n)
{
pos = gcnew array<double>(3);
pos[0] = x; pos[1] = y; pos[2] = z;
}
};
Create an interior pointer to elements of the array
#include "stdafx.h"
using namespace System;
ref class MyObject
{
};
int main()
{
array<MyObject^>^ array_of_buf = gcnew array<MyObject^>(10);
for each (MyObject^ bref in array_of_buf)
{
bref = gcnew MyObject();
}
interior_ptr<MyObject^> ptr_buf;
for (ptr_buf = &array_of_buf[0]; ptr_buf <= &array_of_buf[9]; ptr_buf++)
{
MyObject^ buf = *ptr_buf;
// use the MyObject class
}
}
Create an object for each object array position
#include "stdafx.h"
using namespace System;
ref class MyObject
{
};
int main()
{
array<MyObject^>^ array_of_buf = gcnew array<MyObject^>(10);
for each (MyObject^ bref in array_of_buf)
{
bref = gcnew MyObject();
}
interior_ptr<MyObject^> ptr_buf;
for (ptr_buf = &array_of_buf[0]; ptr_buf <= &array_of_buf[9]; ptr_buf++)
{
MyObject^ buf = *ptr_buf;
// use the MyObject class
}
}
Declare, create, and initialize a 1D managed array
#include "stdafx.h"
int main()
{
array<int>^ managed_array = gcnew array<int>(2) { 10, 20 };
}
Declare, create, and initialize a 1D native array
#include "stdafx.h"
int main()
{
int native_array[2] = { 10, 20 };
}
Declare, create, and initialize a 2D managed array
#include "stdafx.h"
int main()
{
array<int, 2>^ managed_array_2D = gcnew array<int, 2>(2, 2)
{ { 1, 0 }, { 0, 1 } };
}
Declare, create, and initialize a 2D native array
#include "stdafx.h"
int main()
{
int native_array_2D[2][2] = { { 1, 0 }, { 0, 1 } };
}
Get array length
#include "stdafx.h"
using namespace System;
int main()
{
array<String^>^ stringArray = gcnew array<String^>(5){ "one", "two" };
for (int i = 0; i < stringArray->Length; i++)
{
Console::WriteLine( stringArray[i] );
}
Console::WriteLine("End.");
}
Loop over the array with the interior pointer
#include "stdafx.h"
using namespace System;
ref class MyObject
{
};
int main()
{
array<MyObject^>^ array_of_buf = gcnew array<MyObject^>(10);
for each (MyObject^ bref in array_of_buf)
{
bref = gcnew MyObject();
}
interior_ptr<MyObject^> ptr_buf;
for (ptr_buf = &array_of_buf[0]; ptr_buf <= &array_of_buf[9]; ptr_buf++)
{
MyObject^ buf = *ptr_buf;
// use the MyObject class
}
}
Object array with non default constrctuctor
#include "stdafx.h"
using namespace System;
ref class C
{
public:
C(int i) { Value = i; }
property int Value;
};
int main()
{
array<C^>^ array_C = { gcnew C(0), gcnew C(1), gcnew C(2)};
Console::WriteLine( " {0}, {1}, {2} ", array_C[0]->Value,
array_C[1]->Value, array_C[2]->Value);
}
Shallow copy creates another name for the array
#include "stdafx.h"
using namespace System;
int main()
{
array<int>^ array1 = { 0, 1, 2};
array<int>^ array2 = array1;
}
Single dimension Arrays
#include "stdafx.h"
using namespace System;
void main()
{
array<int>^ a = gcnew array<int>(4);
array<String^>^ b = gcnew array<String^>(4);
for (int i = 0; i < a->Length; i++)
{
a[i] = i;
}
for (int i = 0; i < b->Length; i++)
{
b[i] = a[i].ToString();
}
for (int i = 0; i < b->Length; i++)
{
Console::WriteLine(b[i]);
}
}
Sort array with Array.Sort
#include "stdafx.h"
using namespace System;
int main()
{
array<int>^ array1 = gcnew array<int>(10){ 2, 7, 9, 6, 4, 1, 9, 5, 0, 10 };
Array::Sort(array1);
for each (int i in array1)
{
Console::Write("{0} ", i);
}
// Search for one of the values
int index = Array::BinarySearch( array1, 115);
if (index >= 0 )
Console::WriteLine( "Found {0} at position {1}.", array1[index], index );
else
Console::WriteLine(" Not Found. ");
}
String arrays length
#include "stdafx.h"
using namespace System;
int main()
{
array<String^>^ string_array = gcnew array<String^>(2) { "first", "second" } ;
for (int i = 0; i < string_array->Length; i++)
{
Console::WriteLine( string_array[i] );
}
}
Use variables in the initializer list
#include "stdafx.h"
int main()
{
array<int>^ array_int1 = { 0, 1, 2 };
array<int>^ array_int2 = gcnew array<int> { 0, 1, 2 };
int i = 1, j = 2, k = 3;
array<int>^ array_int3 = { i, j, k };
}
Using Array.BinarySearch to search an element in an array
#include "stdafx.h"
using namespace System;
int main()
{
array<int>^ array1 = gcnew array<int>(10){ 2, 7, 9, 6, 4, 1, 9, 5, 0, 10 };
Array::Sort(array1);
for each (int i in array1)
{
Console::Write("{0} ", i);
}
// Search for one of the values
int index = Array::BinarySearch( array1, 115);
if (index >= 0 )
Console::WriteLine( "Found {0} at position {1}.", array1[index], index );
else
Console::WriteLine(" Not Found. ");
}
Using Array::Copy to copy array
#include "stdafx.h"
using namespace System;
int main()
{
array<int>^ array1 = { 0, 1, 2};
array<int>^ array2 = array1;
array<int>^ array3 = gcnew array<int>(3);
Array::Copy(array1, array3, array1->Length);
array3[0] = 200;
Console::WriteLine( "{0} {1} {2}", array1[0], array1[1], array1[2]);
}
Using array.Equals to test if two arrays are equal
1. <A href="/Code/Cpp.net/Collections/SingledimensionArrays.htm">Single dimension Arrays</a> 2. <A href="/Code/Cpp.net/Collections/Usinggcnewtocreatearray.htm">Using gcnew to create array</a> 3. <A href="/Code/Cpp.net/Collections/Declarecreateandinitializea1Dnativearray.htm">Declare, create, and initialize a 1D native array</a> 4. <A href="/Code/Cpp.net/Collections/Declarecreateandinitializea1Dmanagedarray.htm">Declare, create, and initialize a 1D managed array</a> 5. <A href="/Code/Cpp.net/Collections/Declarecreateandinitializea2Dnativearray.htm">Declare, create, and initialize a 2D native array</a> 6. <A href="/Code/Cpp.net/Collections/Declarecreateandinitializea2Dmanagedarray.htm">Declare, create, and initialize a 2D managed array</a> 7. <A href="/Code/Cpp.net/Collections/Arrayinitializing.htm">Array initializing</a> 8. <A href="/Code/Cpp.net/Collections/Arrayforeach.htm">Array for each</a> 9. <A href="/Code/Cpp.net/Collections/Shallowcopycreatesanothernameforthearray.htm">Shallow copy creates another name for the array</a> 10. <A href="/Code/Cpp.net/Collections/UsingArrayCopytocopyarray.htm">Using Array::Copy to copy array</a> 11. <A href="/Code/Cpp.net/Collections/Arrayequalitytest.htm">Array equality test</a> 12. <A href="/Code/Cpp.net/Collections/classwithmanagedarray.htm">class with managed array</a> 13. <A href="/Code/Cpp.net/Collections/Getarraylength.htm">Get array length</a> 14. <A href="/Code/Cpp.net/Collections/SortarraywithArraySort.htm">Sort array with Array.Sort</a> 15. <A href="/Code/Cpp.net/Collections/UsingArrayBinarySearchtosearchanelementinanarray.htm">Using Array.BinarySearch to search an element in an array</a> 16. <A href="/Code/Cpp.net/Collections/Usingforeachtoloopthroughanarray.htm">Using for each to loop through an array</a> 17. <A href="/Code/Cpp.net/Collections/Arraysiterators.htm">Arrays iterators</a> 18. <A href="/Code/Cpp.net/Collections/Createanobjectforeachobjectarrayposition.htm">Create an object for each object array position</a> 19. <A href="/Code/Cpp.net/Collections/Loopoverthearraywiththeinteriorpointer.htm">Loop over the array with the interior pointer</a> 20. <A href="/Code/Cpp.net/Collections/Createaninteriorpointertoelementsofthearray.htm">Create an interior pointer to elements of the array</a> 21. <A href="/Code/Cpp.net/Collections/Arrayinitializationwithoutgcnew.htm">Array initialization without gcnew</a> 22. <A href="/Code/Cpp.net/Collections/Usinggcnewtocreatearray.htm">Using gcnew to create array</a> 23. <A href="/Code/Cpp.net/Collections/Objectarraywithnondefaultconstrctuctor.htm">Object array with non default constrctuctor</a> 24. <A href="/Code/Cpp.net/Collections/Assignvaluefromnativearraytomanagedarray.htm">Assign value from native array to managed array</a> 25. <A href="/Code/Cpp.net/Collections/Stringarrayslength.htm">String arrays length</a> 26. <A href="/Code/Cpp.net/Collections/Usevariablesintheinitializerlist.htm">Use variables in the initializer list</a>
Using for each to loop through an array
#include "stdafx.h"
using namespace System;
int main()
{
array<int>^ array1 = gcnew array<int>(10){ 2, 7, 9, 6, 4, 1, 9, 5, 0, 10 };
Array::Sort(array1);
for each (int i in array1)
{
Console::Write("{0} ", i);
}
// Search for one of the values
int index = Array::BinarySearch( array1, 115);
if (index >= 0 )
Console::WriteLine( "Found {0} at position {1}.", array1[index], index );
else
Console::WriteLine(" Not Found. ");
}
Using gcnew to create array
#include "stdafx.h"
using namespace System;
int main()
{
array<String^>^ stringArray = gcnew array<String^>(5){ "one", "two" };
for (int i = 0; i < stringArray->Length; i++)
{
Console::WriteLine( stringArray[i] );
}
Console::WriteLine("End.");
}