Visual C++ .NET/Collections/Array
Содержание
- 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
<source lang="csharp">
- 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."); } } </source>
Array for each
<source lang="csharp">
- 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); }
}
</source>
Array initialization without gcnew
<source lang="csharp">
- 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 };
}
</source>
Array initializing
<source lang="csharp">
- 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 };
}
</source>
Arrays iterators
<source lang="csharp">
- 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") ); }
}
</source>
Assign value from native array to managed array
<source lang="csharp">
- 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;
}
</source>
class with managed array
<source lang="csharp">
- 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; }
};
</source>
Create an interior pointer to elements of the array
<source lang="csharp">
- 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 }
}
</source>
Create an object for each object array position
<source lang="csharp">
- 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 }
}
</source>
Declare, create, and initialize a 1D managed array
<source lang="csharp">
- include "stdafx.h"
int main() {
array<int>^ managed_array = gcnew array<int>(2) { 10, 20 };
}
</source>
Declare, create, and initialize a 1D native array
<source lang="csharp">
- include "stdafx.h"
int main() {
int native_array[2] = { 10, 20 };
}
</source>
Declare, create, and initialize a 2D managed array
<source lang="csharp">
- include "stdafx.h"
int main() {
array<int, 2>^ managed_array_2D = gcnew array<int, 2>(2, 2) { { 1, 0 }, { 0, 1 } };
}
</source>
Declare, create, and initialize a 2D native array
<source lang="csharp">
- include "stdafx.h"
int main() {
int native_array_2D[2][2] = { { 1, 0 }, { 0, 1 } };
}
</source>
Get array length
<source lang="csharp">
- 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.");
}
</source>
Loop over the array with the interior pointer
<source lang="csharp">
- 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 }
}
</source>
Object array with non default constrctuctor
<source lang="csharp">
- 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);
}
</source>
Shallow copy creates another name for the array
<source lang="csharp">
- include "stdafx.h"
using namespace System; int main() {
array<int>^ array1 = { 0, 1, 2};
array<int>^ array2 = array1;
}
</source>
Single dimension Arrays
<source lang="csharp">
- 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]); }
}
</source>
Sort array with Array.Sort
<source lang="csharp">
- 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. ");
}
</source>
String arrays length
<source lang="csharp">
- 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] ); }
}
</source>
Use variables in the initializer list
<source lang="csharp">
- 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 };
}
</source>
Using Array.BinarySearch to search an element in an array
<source lang="csharp">
- 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. ");
}
</source>
Using Array::Copy to copy array
<source lang="csharp">
- 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]);
}
</source>
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
<source lang="csharp">
- 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. ");
}
</source>
Using gcnew to create array
<source lang="csharp">
- 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.");
}
</source>