Strings.cpp
上传用户:sz0451
上传日期:2022-07-29
资源大小:256k
文件大小:2k
- // This is the main project file for VC++ application project
- // generated using an Application Wizard.
- #include "stdafx.h"
- #using <mscorlib.dll>
- using namespace System;
- using namespace System::Collections;
- // This is the entry point for this application
- #ifdef _UNICODE
- int wmain(void)
- #else
- int main(void)
- #endif
- {
- // Create an array of strings
- String* sa[] = { S"Dog", S"Cat", S"Elephant", S"Gerbil", S"Dog",
- S"Horse", S"Pig", S"Cat" };
- // Check the length
- Console::WriteLine("sa has length {0}", __box(sa->get_Length()));
- // Search for a value
- String* s = S"Dog";
- int pos = Array::IndexOf(sa, s);
- Console::WriteLine("Index of s in sa is {0}", __box(pos));
- // Search for the next occurrence
- pos = Array::IndexOf(sa, s, pos+1);
- Console::WriteLine("Next index of s in sa is {0}", __box(pos));
- // Sort the array and print its contents
- Console::WriteLine();
- Console::WriteLine(S"Sorted array:");
- Array::Sort(sa);
- Array::Reverse(sa);
- for (int i=0; i<sa->get_Length(); i++)
- Console::WriteLine(sa[i]);
- // Sort the array with an array of keys and print it again.
- Int32 keys[] = { 6, 4, 3, 5, 2, 2, 1, 1 };
- Array::Sort(keys, sa);
- Console::WriteLine();
- Console::WriteLine(S"Sorted by key:");
- for (int i=0; i<sa->get_Length(); i++)
- Console::WriteLine(sa[i]);
- // Use an enumerator to iterate over the array and print its contents
- Console::WriteLine();
- Console::WriteLine(S"Using an enumerator:");
- IEnumerator* ie = sa->GetEnumerator();
- while (ie->MoveNext())
- Console::WriteLine(ie->Current);
-
- return 0;
- }