2017-02-26 85 views
1

我正在研究一個函數,該函數可以找到數組中最小的元素。我試圖使用傳遞參考來修改變量s。我品牌到C++,我不知道我是否通過正確的引用完成傳遞。任何人都可以確認這是做到這一點的正確方法,還是建議更好的方法通過引用傳遞接近最小值函數?修改通過引用傳遞的變量

#include <cstdlib> 
#include <stdlib.h> 
#include <iostream> 

using namespace std; 

int smallestElm(int numArray[], int length, int &smallest); 

int main() { 

    int n[3] = {2,5,3}; 
    int s = 0; 
    int length = 0; 

    cout << smallestElm(n, length, s) << endl; 
} 

int smallestElm(int numArray[], int length, int &smallest) { 
    smallest = numArray[0]; 
    length = sizeof (numArray)/sizeof (int); 
    for (int i = 1; i < length; i++) { 
     if (numArray[i] < smallest) { 
      smallest = numArray[i]; 
     } 
     cout << smallest << endl; 
     return 0; 

    } 
} 
+1

你聽說過std :: min [http://en.cppreference.com/w/cpp/algorithm/min]嗎? – ZivS

+2

你爲什麼要回到身體的循環?它會在查看第二個元素後結束函數... – vu1p3n0x

+1

如果您在for循環中返回,您可能會在找到最小元素之前返回! 爲什麼你有一個長度參數,當你不使用,但重新計算你的函數內的長度? – PeterO

回答

1

是的,這是正確的,你應該能夠自己分辨,通過修改你的主要功能如下:

int main() { 
    int s = 0;  
    // call your function 
    cout << s << endl; // Here you print 's', thus you confirm whether you are right or not 
} 

如果s通過不會改變它的值,那麼你的通行證參考將不正確(因爲s確實在函數體內改變了它的值)。


至於功能上,它的錯誤,因爲它會檢查所有的元素之前返回!因此,修改成這樣的說法對某些其中最小的元素是之前檢查陣列中的所有元素:

#include <stdlib.h> 
#include <iostream> 

using namespace std; 

void smallestElm(int numArray[], size_t length, int &smallest); 

int main() { 

    int n[] = {2,5,3}; // size is not needed, it's automatically computed by the compiler 
    int s = 0; 
    size_t length = 3; 

    smallestElm(n, length, s); 
    cout << "smallest element = " << s << endl; 
    return 0; 
} 

void smallestElm(int numArray[], size_t length, int &smallest) { 
    smallest = numArray[0]; 
    for (int i = 1; i < length; i++) { 
     if (numArray[i] < smallest) { 
      smallest = numArray[i]; 
     } 
     cout << smallest << endl; 
    } 
} 

輸出:

Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall main.cpp 
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
2 
2 
smallest element = 2 

不要忘了, STL提供min_element,你可以使用這樣的:

#include <algorithm> 
#include <iostream> 

using namespace std; 

int main() { 

    int n[] = {2,5,3}; 
    int *s = std::min_element(n, n + 3); // 3 size of the array 
    cout << "smallest element = " << *s << endl; 
    return 0; 
} 

輸出:

Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall main.cpp 
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
smallest element = 2 
+1

難以置信!謝謝你的詳細回覆。我從中學到了很多東西。 –

2

誰能確認這是要做到這一點

是的,這是聲明引用參數的正確方法是正確的方法。是的,你可以通過引用來修改對象。

或暗示更好的方法來接近最小值功能...

一種更好的方式將無疑是返回而不是修改一個參數的最小值。現在函數總是返回0,這似乎沒用。

...與通過引用傳遞

這是一個愚蠢的想法,但你的做法是按引用傳遞正確的方法。該函數本身有多個錯誤。

  • 它似乎總是在第一次迭代後返回,所以它總是會找到前2個元素中的一個爲「最小」。
  • 從不使用int length參數的值。它在使用前被覆蓋。
  • sizeof (numArray)返回指針numArray的大小,它與指向數組的大小沒有任何關係。
  • 此功能始終使用numArray[0],因此如果length == 0將具有未定義的行爲。
0

這是正確的代碼,但還有另一種方式:使用指針爲int,到函數參數和與可變小號內存中的地址調用此,如下面的示例顯示:

#include <stdlib.h> 
#include <iostream> 

using namespace std; 

void smallestElm(int numArray[], size_t length, int *smallest); 

int main() { 

int n[] = {2,5,3}; // size is not needed, it's automatically computed by the compiler 
int s = 0; 
size_t length = 3; 

smallestElm(n, length, &s); 
cout << "smallest element = " << s << endl; 
return 0; 
} 

void smallestElm(int numArray[], size_t length, int *smallest) { 
*smallest = numArray[0]; 
for (int i = 1; i < length; i++) { 
    if (numArray[i] < *smallest) { 
     *smallest = numArray[i]; 
    } 
    cout << *smallest << endl; 
} 
}