设为首页 加入收藏

TOP

4.6.2 CLR数组(4)
2013-10-07 12:28:07 来源: 作者: 【 】 浏览:81
Tags:4.6.2 CLR 数组

4.6.2  CLR数组(4)

我们将names和weights数组排序,用names数组来决定最终的顺序。然后,在for each循环中针对toBeFound数组中的每个姓名搜索names数组。我们依次将toBeFound数组中的每一个姓名赋给循环变量name。在该循环中,用下面这条语句查找当前的姓名:

  1. result = Array::BinarySearch(names, name);          // Search names array 

该语句返回names数组中包含name的那个元素的索引值,或者未找到name时返回一个负整数。然后,我们在if语句中测试结果并产生输出:

  1. if(result<0)        // Check the result  
  2. Console::WriteLine(L"{0} was not found.", name);  
  3. else  
  4. Console::WriteLine(L"{0} weighs {1} lbs.", name, weights[result]); 

因为weights数组的顺序取决于names数组的顺序,所以我们可以用names数组中包含name的那个元素的索引值-- 即result来索引weights数组。从输出中可以看出,在names数组中没有找到"Fred"。

当对分搜索操作失败时,函数返回的数值决不是任意的负数。事实上,该整数是第一个大于搜索目标的元素索引值的按位补码,如果没有大于搜索目标的元素,该整数则是Length属性的按位补码。知道了这一点,我们就可以使用BinarySearch()函数求出为了仍然保持元素的正确顺序应该将新对象插入到数组的什么位置。假设我们希望在names数组中插入"Fred"。使用下面这些语句,可以找出应该插入"Fred"的索引位置:

  1. array<String^>names = {"Jill", "Ted", "Mary", "Eve", "Bill",  
  2. "Al", "Ned", "Zoe", "Dan", "Jean"};  
  3. Array::Sort(names);     // Sort the array  
  4. String^ name = L"Fred";  
  5. int position = Array::BinarySearch(names, name);  
  6. if(position<0)      // If it is negative  
  7. position = ~position;       // flip the bits to get the insert index 

如果搜索结果为负数,则将所有位反转就是应该插入新姓名的索引位置。如果结果是正数,则说明新姓名与该位置的姓名相同,因此我们可以直接将结果用作新位置。

现在,我们可以将names数组复制到比它多一个元素的新数组中,并使用position的值在适当的位置插入name:

  1. array<String^>newNames = gcnew array<String^>(names->Length+1);  
  2.  
  3. // Copy elements from names to newNames  
  4. for(int i = 0 ; i<position ; i++)  
  5. newNames[i] = names[i];  
  6.  
  7. newNames[position] = name;  // Copy the new element  
  8.  
  9. if(position<names->Length)  // If any elements remain in names  
  10. for(int i = position ; i<names->Length ; i++)  
  11. newNames[i+1] = names[i];           // copy them to newNames 

这段代码首先创建一个比旧数组多一个元素的新数组,然后将旧数组中的元素逐一复制到新数组中,一直复制到索引位置position-1。此刻先复制新姓名,接着再复制旧数组中剩余的元素。为了抛弃旧数组,我们只需写这样一条语句:

  1. names = nullptr

3. 多维数组

我们可以创建二维或多维数组,数组的最大维数是32,应该能够适应大多数情况。我们在尖括号内元素类型后面指定数组的维数,中间用逗号分开。数组的维数默认是1,因此我们在之前的示例中不需要指定维数。下面是创建二维整型数组的方法:

  1. array<int, 2>values = gcnew array<int, 2>(4, 5); 

这条语句创建一个四行五列的二维数组,因此总共是20个元素。为访问多维数组的元素,我们需要指定多个索引值-- 每一维一个,这些索引值必须写在数组名后面的方括号内,中间以逗号分开。下面是给一个整型二维数组的元素赋值的方法:

  1. int nrows = 4;  
  2. int ncols = 5;  
  3. array<int, 2>values = gcnew array<int, 2>(nrows, ncols);  
  4. for(int i = 0 ; i<nrows ; i++)  
  5. for(int j = 0 ; j<ncols ; j++)  
  6. values[i,j] = (i+1)*(j+1); 

嵌套的循环逐一处理该数组的所有元素。外部循环处理每一行,内部循环处理当前行的每一个元素。我们看到,该代码段将表达式(i+1)*(j+1)的值赋给各个元素。因此,第一行元素将是1,2,3,4,5;第二行元素将是2,4,6,8,10;依此类推,最后一行元素将是4,8,12,16,20。

读者肯定已经注意到,这里访问二维数组元素的符号不同于为本地C++(www.cppentry.com)数组使用的符号。C++(www.cppentry.com)/CLI数组不像本地C++(www.cppentry.com)数组那样,是数组的数组,而是真正的二维数组。我们不能对二维C++(www.cppentry.com)/CLI数组使用一个索引值,因为这种数组是元素的二维数组,而非数组的数组。前面曾经讲过,数组的维数被称为等级,因此上面代码段中values数组的等级是2。当然,我们还可以定义等级为3或更大(一直到32)的C++(www.cppentry.com)/CLI数组。相反,本地C++(www.cppentry.com)数组的等级实际上始终是1,因为二维或多维的本地C++(www.cppentry.com)数组事实上是数组的数组。稍后我们将看到,在C++(www.cppentry.com)/CLI中也可以定义数组的数组。

下面的示例中介绍了如何使用多维数组。

试一试:使用多维数组

该CLR控制台示例在二维数组中创建12×12的乘法表。

  1. // Ex4_16.cpp : main project file.  
  2. // Using a two-dimensional array  
  3.  
  4. #include "stdafx.h"  
  5.  
  6. using namespace System;  
  7.  
  8. int main(array<System::String ^> ^args)  
  9. {  
  10. const int SIZE = 12;  
  11. array<int, 2>products = gcnew array<int, 2>(SIZE,SIZE);  
  12.  
  13. for (int i = 0 ; i < SIZE ; i++)  
  14. for(int j = 0 ; j < SIZE ; j++)  
  15. products[i,j] = (i+1)*(j+1);  
  16.  
  17. Console::WriteLine(L"Here is the {0} times table:", SIZE);  
  18.  
  19. // Write horizontal divider line  
  20. for(int i = 0 ; i <= SIZE ; i++)  
  21. Console::Write(L"_____");  
  22. Console::WriteLine();           // Write newline  
  23.  
  24. // Write top line of table  
  25. Console::Write(L" |");  
  26. for(int i = 1 ; i <= SIZE ; i++)  
  27. Console::Write(L"{0,3} |", i);  
  28. Console::WriteLine();       // Write newline  
  29.  
  30. // Write horizontal divider line with verticals  
  31. for(int i = 0 ; i <= SIZE ; i++)  
  32. Console::Write(L"____|");  
  33. Console::WriteLine();       // Write newline  
  34.  
  35. // Write remaining lines  
  36. for(int i = 0 ; i<SIZE ; i++)  
  37. {  
  38. Console::Write(L"{0,3} |", i+1);  
  39. for(int j = 0 ; j<SIZE ; j++)  
  40. Console::Write(L"{0,3} |", products[i,j]);  
  41.  
  42. Console::WriteLine();           // Write newline  
  43. }  
  44.  
  45. // Write horizontal divider line  
  46. for(int i = 0 ; i <= SIZE ; i++)  
  47. Console::Write(L"_____");  
  48. Console::WriteLine();   // Write newline  
  49. return 0;  

该示例应该产生下面的输出:

  1. Here is the 12 times table: 
 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇4.6.2 CLR数组(5) 下一篇4.6.2 CLR数组(3)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: