小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C++ Builder中的System::DynamicArray動態(tài)數(shù)組

 quasiceo 2014-01-01
分類: C++Builder程序設計 2011-04-08 18:23 1784人閱讀 評論(0) 收藏 舉報

C++Builder里面有動態(tài)數(shù)組,使用如下:

  1. //==============================================  
  2.   
  3. //數(shù)組長度  
  4.   
  5. DynamicArray<int> arrayOfInt;  
  6. arrayOfInt.Length = 10;  
  7. cout << "ArrayLength: " << arrayOfInt.Length << endl;  
  8.   
  9. //==============================================  


釋放數(shù)組,只有把長度設置為0即可,不可使用delete刪去:

 

  1. //==============================================  
  2.   
  3. arrayOfInt.Length = 0;  
  4.   
  5. //==============================================  


訪問數(shù)據:

  1. void InitArray(DynamicArray<char> &c_array)  
  2. {  
  3.   c_array[0] = 'A';  
  4.   c_array[1] = 'B';  
  5.   cout << "Third char is: " << c_array[2];  
  6. }  
  7.   
  8. //==============================================  


 Low和 High 屬性表示數(shù)組的上下邊界

例如數(shù)組求和的函數(shù):

  1. int TotalArray(const DynamicArray<int>& arrayOfInt)  
  2. {  
  3.   int total=0;  
  4.   for (int i=arrayOfInt.Low; i<=arrayOfInt.High; i++)  
  5.     total += arrayOfInt[i];  
  6.   return total;  
  7. }  
  8. 當然也可以這樣:  
  9.   
  10. for (int i=0; i<arrayOfInt.Length; i++)  
  11.   
  12. {  
  13.   
  14. total += arrayOfInt[i];  
  15.   
  16. }  
  17.   
  18. //==================================================  


賦值,比較和復制動態(tài)數(shù)組:

Dynamic Arrays are reference counted. When a DynamicArray is assigned to another, only the reference is assigned (and the reference count adjusted), the content of the source is not copied. Similarly, when two Dynamic Arrays are compared, only the references are compared, not the contents. To copy the contents of a DynamicArray, you must use the Copy (or CopyRange) methods.

  1. //C++ example  
  2.   
  3. void foo(DynamicArray<int> &i_array)  
  4. {  
  5.   DynamicArray<int> temp = i_array;//此處是引用賦值,不復制數(shù)據  
  6.   assert(temp == i_array); // temp 和i_array 指向同一內存數(shù)據塊  
  7.   i_array[0] = 20;//此處改變,實際上就是Temp的值  
  8.   assert(temp[0] == 20); // Temp的值同樣也會改變  
  9.   temp = i_array.Copy(); // 賦值數(shù)組,此時temp 和i_array 指向不同內存塊,  
  10. //但是這兩個內存所含數(shù)據相同  
  11.   temp[0] = 10;//此時改變temp的值不會改變i_array的值  
  12.   assert(temp[0] != i_array[0]); // Above assignment did not  
  13.                                  // modify i_array.  
  14. }  
  15. //=====================================================  

Multidimensional dynamic arrays(多維數(shù)組)
例如2維數(shù)組,注意該2維數(shù)組可以不是方陣數(shù)組,
即數(shù)組可以由10行,但每一行的元素個數(shù)可以不相同
  1. typedef DynamicArray< DynamicArray < AnsiString > > T2DStringArray;  
  2. void foo(T2DStringArray &s_array)  
  3. {  
  4.   SetLength(s_array, 10);  
  5.   for (int i=0; i<s_array.Length; i++)  
  6.   { // Set lengths of second dimensions.(NOTE: non-rectangular)  
  7.     SetLength(s_array[i], i+1);  
  8.     for (int j=0; j<s_array[i].Length; j++)  
  9.       s_array[i][j] = itoa(i*10+j);  
  10.   }  
  11. }  
  12.   
  13. //===================================================== 

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多