【问题标题】:How to return pointer using conditional statement?如何使用条件语句返回指针?
【发布时间】:2012-10-18 15:19:27
【问题描述】:

以下代码将第二个数字作为最大值输出。 如果您需要任何其他信息,请告诉我。

#include <iostream>                                                     
#include <cstdlib>
using namespace std;
double *ComputeMaximum(const double *Max, const double *Min);

double *ComputeMaximum(const double *Max, const double *Min)
{
    return ((double *)((&Max > &Min) ? Max : Min));
}

int main(void)
{
    double *max;
    double Initial, Secondary;


    cout << "Enter the number followed by space then another number: ";
    cin >> Initial;
    cout << "\nIn-" << Initial;
    cin >> Secondary;
    cout << "\nSe-" << Secondary;
    //cout >> "Of " >> Inital >> "and " >> Secondary;
    //cout >> "the maximum is " >>
    max = ComputeMaximum((double*)&Initial,(double*)&Secondary);
    cout << "\nmax" << *max;
    return 0;
}

下一个代码输出第一个数字作为最大值

#include <iostream>                                                     
#include <cstdlib>
using namespace std;
double *ComputeMaximum(const double *Max, const double *Min);

double *ComputeMaximum(const double *Max, const double *Min)
{
    return ((double *)((Max > Min) ? Max : Min));  // Here is the difference(& missing)
}

int main(void)
{
    double *max;
    double Initial, Secondary;


    cout << "Enter the number followed by space then another number: ";
    cin >> Initial;
    cout << "\nIn-" << Initial;
    cin >> Secondary;
    cout << "\nSe-" << Secondary;
    //cout >> "Of " >> Inital >> "and " >> Secondary;
    //cout >> "the maximum is " >>
    max = ComputeMaximum((double*)&Initial,(double*)&Secondary);
    cout << "\nmax" << *max;
    return 0;
}

做错了什么?我只需要最大值,而不是第二个或第一个输入。 我得到了答案。谢谢大家。

这里是:

#include <iostream>                                                     
#include <cstdlib>
using namespace std;
double *ComputeMaximum(const double *Max, const double *Min);

double *ComputeMaximum(const double *Max, const double *Min)
{
    return (double*)((*Max > *Min) ? Max : Min);
}

int main(void)
{
    double *max;
    double Initial, Secondary;


    cout << "Enter the number followed by space then another number: ";
    cin >> Initial;
    cout << "\nIn-" << Initial;
    cin >> Secondary;
    cout << "\nSe-" << Secondary;
    //cout >> "Of " >> Inital >> "and " >> Secondary;
    //cout >> "the maximum is " >>
    max = ComputeMaximum(&Initial, &Secondary);
    cout << "\nmax" << *max;
    return 0;
}

【问题讨论】:

  • 根据您的 ComputeMaximum 声明,您不需要编写 ComputeMaximum((double*)&Initial,(double*)&Secondary)。相反,您可以简单地编写:ComputeMaximum(&Initial, &Secondary)。

标签: c++ pointers conditional-statements


【解决方案1】:

您正在比较地址。正确的做法是:

double *ComputeMaximum(const double *Max, const double *Min)
{
    return *Max > *Min ? Max : Min;
}

您的版本:

(Max > Min)

比较指针本身,并且

(&Max > &Min)

比较指针的地址,这又是错误的。

另外,您不需要指针,请注意您有可以使用的std::max

【讨论】:

    【解决方案2】:
    double *ComputeMaximum(const double *Max, const double *Min)
    {
        return *Max > *Min ? Max : Min;
    }
    

    请注意,我使用*Max*Min 而不是&amp;Max&amp;Min,即取消引用,而不是获取地址!另请注意,对于已经是所需类型的表达式,您对double* 有很多不必要的强制转换。一个例子是您的 ComputeMaximum 函数体。另一个例子

    max = ComputeMaximum((double*)&Initial,(double*)&Secondary);
    

    因为InitialSecondarydouble 类型,&amp;Initial&amp;Secondarydouble* 类型所以绝对不需要丑陋的不必要的演员表。只需使用

    max = ComputeMaximum(&Initial,&Secondary);
    

    在其他地方也是如此。

    我强烈建议你阅读a good book on C++

    【讨论】:

    • 有哪些好的 C++ 和 C 书籍? NVM 刚刚注意到链接
    • @user1781382: 我特地贴了一个链接 :) 我首先推荐 S.Lippmann 的 C++ Primer,第 4 版或更高版本
    • @user1781382:另外请注意,没有一本书同时适用于 C 和 C++。这些是具有不同范式、方法、技术和哲学的不同语言。所以先选一门语言再学,不要把两者混为一谈
    • 我必须在课堂上同时学习这两本书,但我知道我所拥有的书并没有太大帮助。
    • @user1781382:如果你的课程叫 C/C++,那就太可怕了,因为它就像有一个叫西班牙语/意大利语的语言课。你找不到一本关于西班牙语/意大利语的书。
    【解决方案3】:

    你为什么首先使用指针?甚至不需要。

    以下是更好的实现:

    double ComputeMaximum(double a, double b)
    {
        return a > b ? a : b;
    }
    

    或者如果你想做这样的事情:

    ComputeMaximum(x,y) = 100; //modify the one which is maximum
    

    那么参考就是你所需要的:

    double & ComputeMaximum(double & a, double & b)
    {
        return a > b ? a : b;
    }
    

    顺便说一句,您可能希望看到标准库中的std::max(和std::min)。

    【讨论】:

      【解决方案4】:

      它们都不返回实际的最大值。

      您正在传递指向双精度的指针。所以变量 Min,Max 的值是一个指向 double 的地址。

      &amp;Max &gt; &amp;Min ... 这将比较函数本地变量 Max,Min 的地址。

      Max &gt; Min ... 这将比较 Min 和 Max 指向的地址(只是地址编号)。

      *Max &gt; *Min ...这将取消引用指针,并比较它们指向的对象。这就是你想要做的......

      return ((double *)((*Max > *Min) ? Max : Min));
      

      【讨论】:

      • 为什么需要在三元表达式前面显式地进行双重转换?
      • 你不是真的,两个返回值属于同一类型,编译器会正确推断它们。只是从原来的复制粘贴...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      相关资源
      最近更新 更多