4.6.2 CLR数组(2)
该示例的典型输出如下所示:
The array contains the following values:
|
30.38 < xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> |
73.93 |
29.82 |
93.00 |
78.14 |
|
89.53 |
75.87 |
5.98 |
45.29 |
89.83 |
|
5.25 |
53.86 |
11.40 |
3.34 |
83.39 |
|
69.94 |
82.43 |
43.05 |
32.87 |
59.50 |
|
58.89 |
96.69 |
34.67 |
18.81 |
72.99 |
|
89.60 |
25.53 |
34.00 |
97.35 |
55.26 |
|
52.64 |
90.85 |
10.35 |
46.14 |
82.03 |
|
55.46 |
93.26 |
92.96 |
85.11 |
10.55 |
|
50.50 |
8.10 |
29.32 |
82.98 |
76.48 |
|
83.94 |
56.95 |
15.04 |
21.94 |
24.81 |
The maximum value in the array is 97.35
示例说明
我们首先创建一个可存储50个double类型数值的数组:
- array<double>^ samples = gcnew array<double>(50);
数组变量samples必须是跟踪句柄,因为CLR数组是在可回收垃圾的堆上创建的。
通过下面的语句,我们用double类型的伪随机数给数组元素赋值:
- Random^ generator = gcnew Random;
- for(int i = 0 ; i< samples->Length ; i++)
- samples[i] = 100.0*generator->NextDouble();
第一条语句在CLR堆上创建一个Random类型的对象。Random对象具有一些可生成伪随机数的函数。我们在上面的循环中使用NextDouble()函数,它返回0.0~1.0之间的double类型的随机数。通过使其与100.0相乘,我们将得到0.0~100.0之间的数值。for循环将随机数存入数组samples的各个元素中。
注意:
Random对象的Next()函数可返回int类型的随机非负数。如果调用Next()函数时提供一个整数作为参数,该函数将返回小于给定参数的随机非负数。我们也可以提供两个整数参数-- 它们表示将返回的随机整数的最小值和最大值。
下一个循环输出samples数组的内容,每行5个元素:
- Console::WriteLine(L"The array contains the following values:");
- for(int i = 0 ; i< samples->Length ; i++)
- {
- Console::Write(L"{0,10:F2}", samples[i]);
- if((i+1)%5 == 0)
- Console::WriteLine();
- }
在该循环内,我们将各个元素的值输出到控制台上,字段宽度为10个字符,其中有两个小数位。指定字段宽度可以确保这些数值按列对齐。另外,只要表达式(i+1)%5等于0,我们就将换行符写到输出流中。这种情况每输出5个元素值之后就出现一次,因此我们得到的输出是每行5个元素。
最后,我们需求出最大值是多少:
- double max = 0;
- for each(double sample in samples)
- if(max < sample)
- max = sample;
这里使用for each循环仅仅是为了表明可以这样。该循环依次将max与每个元素值进行比较,只要某个元素值大于max的当前值,就使max等于该元素值,因此最终max的值就是最大的元素值。
如果除了最大的元素值以外,我们还希望记录该元素的索引位置,那么可以使用for循环:
- double max = 0;
- int index = 0;
- for (int i = 0 ; i < samples->Length ; i++)
- if(max < samples[i])
- {
- max = samples[i];
- iindex = i;
- }
1. 一维数组的排序
System命名空间中Array类定义了一个可将一维数组的元素以升序排列的Sort()函数。为排序某个数组,我们只需将该数组的句柄传递给Sort()函数即可。下面是一个示例:
- array<int>^ samples = { 27, 3, 54, 11, 18, 2, 16};
- Array::Sort(samples); // Sort the array elements
-
- for each(int value in samples) // Output the array elements
- Console::Write(L"{0, 8}", value);
- Console::WriteLine();
调用Sort()函数将使samples数组中元素的值重新以升序排列。执行该代码段的结果如下:
- 2 3 11 16 18 27 54
我们还可以将数组的部分元素排序,方法是给Sort()函数提供两个参数,指出要排序的元素中第一个元素的索引值和总数量。例如:
- array<int>^ samples = { 27, 3, 54, 11, 18, 2, 16};
- Array::Sort(samples, 2, 3); // Sort elements 2 to 4
该语句将samples数组中的三个元素排序,开始的索引值是2。执行这些语句之后,该数组中元素的排列将如下所示:
- 27 3 11 18 54 2 16
如果查阅文档,我们可以发现Sort()函数有若干版本,但本书将介绍一个特别有用的版本。该版本假设我们有两个相关的数组,即第一个数组中的元素是第二个数组中对应元素的键。例如,我们可以将人名存储在一个数组中,将体重信息存储在第二个数组中。该版本的Sort()函数将以升序排列names数组的元素,同时重新调整weights数组中的元素,从而使每个人的体重仍然与其姓名对应。
试一试:对两个相关的数组排序
本示例创建一个存储姓名的数组,并将每个人的体重存储在第二个数组的对应元素中,然后同时将这两个数组排序。代码如下:
- // Ex4_14.cpp : main project file.
- // Sorting an array of keys(the names) and an array of objects(the weights)
-
- #include "stdafx.h"
-
- using namespace System;
-
- int main(array<System::String ^> ^args)
- {
- array<String^>^ names = { "Jill", "Ted", "Mary", "Eve", "Bill", "Al"};
- array<int>^ weights = { 103, 168, 128, 115, 180, 176};
-
- Array::Sort( names,weights); // Sort the arrays
- for each(String^ name in names) // Output the names
- Console::Write(L"{0, 10}", name);
- Console::WriteLine();
-
- for each(int weight in weights) // Output the weights
- Console::Write(L"{0, 10}", weight);
- Console::WriteLine();
- return 0;
- }