2015-10-14 101 views
1

我有一個函數可以抓取給定數組中的不同數字。我將不同的數字存儲在另一個數組中,但我想訪問getUncommon以外的不同數字,以便我可以進行一些比較和排序。在C++中訪問函數外部的陣列

如果沒有在C++中使用全局變量,這可能嗎?

#include <iostream> 

using namespace std; 

void getUncommon(int* iAry, int size) { 
    const int size2 = 10; 
    int* tmpAry = new int[size2]; 
    int totalCount[size2] = { 0 }; 
    int currentCount[size2] = { 0 }; 
    int totalUncommon = 0; 
    int i, j; 
    int rareDigits[size2] = { 0 }; 

    for (i = 0; i < size; i++) { 
     tmpAry[i] = iAry[i]; 
     if (tmpAry[i] < 0) 
      tmpAry[i] *= -1; 

     for (j = 0; j < size2; j++) 
      currentCount[j] = 0; 

     if (tmpAry[i] == 0) { 
      currentCount[0] = 1; 
     } 

     while (tmpAry[i]/10 != 0 || tmpAry[i] % 10 != 0){ 
      currentCount[tmpAry[i] % 10] = 1; 
      tmpAry[i] /= 10; 
     } 

     for (j = 0; j < size2; j++) { 
      totalCount[j] += currentCount[j]; 
     } 
    } 

    for (i = 0; i < size2; i++) { 
     if (totalCount[i] == 1) { 
      totalUncommon++; 
     } 
    } 

    cout << "Total of uncommon digits: " << totalUncommon << endl 
     << "Uncommon digits:\n"; 
    if (totalUncommon == 0) { 
     cout << "\nNo uncommon digits found."; 
    } 
    else { 
     for (i = 0; i < size2; i++) { 
      if (totalCount[i] == 1) { 
       cout << i << endl; 
       rareDigits[i] = totalCount[i]; 
      } 
     } 
    } 
    return; 
} 

int getNumRareDigits(int x) { 
    // I would like to access rareDigits 
    // so that I can pass in an integer 
    // and see if it contains rareDigits[i] to 
    // find the total number of rare digits. 
} 

int main(){ 
    int* my_arry; 
    int size; 
    int i; 

    cout << "How many integers? "; 
    cin >> size; 

    for (i = 0; i < size; i++) { 
     cout << "Enter values #" << i << " : "; 
     cin >> size; 
    } 


    cout << "\nThe original array:" << endl; 
    for (i = 0; i < size; i++) { 
     cout << my_arry[i] << endl; 
    } 

    cout << "\nCalling function -\n" << endl; 

    getUncommon(my_arry, size); 


    return 0; 
} 

如何訪問rareDigits[i]getUncommon

+0

「我在[另一個數組[A]中存儲了另一個數組[A']中不同的數字,但我想訪問外部數字** ['A'或'B'?] ** 'foo()',這樣我就可以做一些比較和排序。「 –

+0

1.使用全局變量或2.在main()中定義rareDigits,然後將其傳遞給其他函數。 –

+0

我的建議 - 簡單地從getUncommon返回一個(稀有數字的)向量。或者,如果你想對它們進行排序,一套。 –

回答

0

可以通過引用傳遞。

void getUncommon(int* iAry, int size, int *outArr, int &outSize) //&outSize make it pass by reference 
{ 
    outArr[0] = 1; 
    outArr[1] = 2; 
    outSize = 2; //Address of outSize and address of outSz same, so they both hold same value 
} 

int main() 
{ 

    int my_arry[10], outArr[10]; 
    int outSz, size; 

    getUncommon(my_arry, size, outArr, outSz); 
    cout<<outSz<<endl; 
    for(int i=0; i<outSz; i++) 
    { 
     cout<<outArr[i]<<" "; 
    } 


    return 0; 
} 

這裏您outArr應通過outSzgetUncommon功能和陣列返回大小內填滿。

您的代碼有其他問題,顯示的UB

您聲明指針

int* my_arry; 

,但你不分配內存。要分配內存,你可以使用免費的內存

delete []my_arry; 
1

如果我正確理解你的問題後,使用

my_arry = new int[10]; 

,問題的真正的心臟是您要訪問從一個局部變量一個外部範圍。在不使用全局的情況下,您的主要方法是傳入要填充的數組。

例如,getUncommon現在可能是這樣的:

void getUncommon(int* iAry, int size, int* rare, int rareSize) { ... 

現在,你可以在未來考慮的一個問題是做什麼如果「罕見的」數組的大小是不知道的前期。爲了解決這個問題,你可能想要使用int **(在getUncommon中分配數組),或者更可能使用類似std :: vector &的東西。