修改引用传递的变量

时间:2017-02-26 20:00:58

标签: c++ reference

我正在处理一个找到数组中最小元素的函数。我正在尝试使用pass by reference修改变量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;

    }
}

3 个答案:

答案 0 :(得分:2)

  

任何人都可以确认这是正确的方法吗

是的,这是声明引用参数的正确方法。是的,您可以通过引用修改对象。

  

或建议更好的方法来接近最小值函数......

更好的方法可能是返回最小值,而不是修改参数。现在,函数总是返回0,这似乎没用。

  

...以参考方式传递

这是一个愚蠢的想法,但你的方法是通过引用传递的正确方法。该函数本身有多个错误。

  • 它似乎总是在第一次迭代后返回,所以它总是会发现前两个元素中的一个是“最小的”。
  • 永远不会使用int length参数的值。它在使用前被覆盖。
  • sizeof (numArray)返回指针numArray的大小,该指针与指向数组的大小没有任何关系。
  • 该函数始终使用numArray[0],因此如果length == 0,它将具有未定义的行为。

答案 1 :(得分: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

答案 2 :(得分:0)

它更正了你的代码,但还有另一种方法:使用指向int 的指针进入函数参数并使用变量 s <的内存地址调用它/ strong>,如下面的示例所示:

#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;
}
}