2015-10-14 39 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 &的东西。