在这种情况下,为函数赋值有什么用

What is the use of assigning a value to the function in this case?

本文关键字:什么 赋值 函数 这种情况下      更新时间:2023-10-16

我无法理解compare(a,b)=-1;在此代码中做了什么。我所看到的只是这个值(-1)被分配给较低的no。变量

#include <iostream>
#include <stdlib.h>
using namespace std;
int &compare(int &c ,int &d) {
   if (c>d)
      return c;
   else
      return d;
} 
int main(int argc, char const *argv[]) {
   int a,b,j;
   std::cin >> a>>b;
   compare(a,b)=-1;
   std::cout <<a<<b<<std::endl;
}

该函数返回对 greater 参数的引用,以便可以对其进行修改。

compare(a,b)返回对a的引用(或 b ) 如果a(或 b ) 大于 b(或 a ),compare(a,b) = -1-1分配给函数结果(最大元素)。

例如:

a = 1;
b = 2;
compare(a,b) = -1;

在此之后,我们有: a=1, b=-1 .