【问题标题】:C++ Sorting the "percentage" of two paired integer arraysC ++对两个成对整数数组的“百分比”进行排序
【发布时间】:2015-11-15 08:21:56
【问题描述】:

我有一个包含 2 个“配对”整数数组 newNumerator[] 和 newDenominator[] 的程序,它们都有 9 个整数。我编写了一个按升序对它们进行排序的函数,但是我不确定它是否有效,因为我还没有成功编译它。我也有一些类型转换的问题。这是函数定义-

void sortData(int *newNumerator[], int *newDenominator[], int newSize)
{
    int temp1;
    int temp2;
    bool swap;
    int count = 0;
    double percentageLeft = 100.0 * static_cast<double>(newNumerator[count]) / newDenominator[count];
    double percentageRight = 100.0 * static_cast<double>(newNumerator[count + 1]) / newDenominator[count + 1];

    do
    {  swap = false;
        for(count = 0; count < (newSize - 1); count++)
        {
            if(percentageLeft > percentageRight)
            {
                temp1 = *newNumerator[count];
                *newNumerator[count] = *newNumerator[count + 1];
                *newNumerator[count + 1] = temp1;

                temp2 = *newDenominator[count];
                *newDenominator[count] = *newDenominator[count + 1];
                *newDenominator[count + 1] = temp2;

                swap = true;
            }
        }
    } while (swap);
}

我遇到的类型转换问题是 percentLeft 和 percentRight 因为 newNumerator[ ] 和 newDenominator[ ] 是整数指针,但要获得它们的“百分比”,我需要它是双精度的。不知道该怎么做。基本上我只需要弄清楚如何解决这个问题,并且还要知道我的功能是否达到了它的目的。任何帮助表示赞赏,如果有什么我可以进一步澄清的,请告诉我

【问题讨论】:

  • 请始终在问题中包含错误消息
  • 现在我得到的唯一一个是“161:38: Static_cast from 'int *' to 'double' is not allowed”

标签: c++ arrays sorting pointers casting


【解决方案1】:

我建议使用 STL 进行排序。让事情变得更简单、更容易理解。

#include <vector>
#include <algorithm>

struct Ratio
{
    int numerator;
    int denominator;

    double toDouble() const
    {
        return static_cast<double>(numerator)
             / static_cast<double>(denominator);
    }

    bool operator < (Ratio const& other) const
    {
        return toDouble() < other.toDouble();
    }
};

void main()
{
    std::vector<Ratio> ratios = { {1, 2}, {5, 7}, {2, 11} };
    std::sort(ratios.begin(), ratios.end());
}

处理原始数组和手动排序几乎总是更乏味且容易出错的方法。

请参阅http://en.cppreference.com/w/cpp/algorithm/sorthttp://en.cppreference.com/w/cpp/container/vector 以供参考

【讨论】:

    【解决方案2】:

    您遇到的主要错误是您给static_cast&lt;&gt; 提供了错误的值。后来你通过取消引用正确地使用它们,但在演员阵容中你错过了。

    你需要的是:

    double percentageLeft = 100.0 * static_cast<double>((*newNumerator)[count]) / *newDenominator[count];
    double percentageRight = 100.0 * static_cast<double>((*newNumerator)[count + 1]) / *newDenominator[count + 1];
    

    添加括号以明确取消引用是什么。

    此外,如果您不更改实际的数组指针,而只更改数组的内容,则可以完全消除取消引用的麻烦。如果将函数定义为

    void sortData(int newNumerator[], int newDenominator[], int newSize)
    

    您可以只使用普通索引,而无需每次使用时取消引用。

    【讨论】:

    • 谢谢,这解决了类型转换问题,但是现在当我调用函数“sortData(newNumerator, newDenominator, newSize);”时,它说调用“sortData”不匹配,说“候选函数不可行:第一个参数没有从 'int' 到 'int **' 的已知转换“我该如何解决这个问题?
    • @Sosa 这意味着你给它一个 int,而不是一个指向 int 数组的指针,所以你需要添加关于如何调用函数以及使用什么参数类型的信息
    • 这里是所有代码的完整的粘贴箱。错误来自第 45 行-pastebin.com/R4LW5a51
    • @Sosa 您在其他功能中使用int[],但为此您选择了int*[]。如果您将代码更改为在此代码中也使用int[],您也将摆脱此错误,因为您将其提供为int[],并且它需要int*[]
    • 我确实尝试过使用 'int[ ]' 但是当我运行程序时,它不会“排序”数据。它只是以相同的顺序输出数组中的数据,而不是所需的升序。知道为什么要这样做吗?
    【解决方案3】:

    你说:

    我有一个包含 2 个“配对”整数数组 newNumerator[] 和 newDenominator[]

    的程序

    但你的函数定义为:

    void sortData(int *newNumerator[], int *newDenominator[], int newSize) 
    

    我认为应该是:

    void sortData(int newNumerator[], int newDenominator[], int newSize) 
    

    如果确实如此,那么以下几行:

    temp1 = *newNumerator[count];
    *newNumerator[count] = *newNumerator[count + 1];
    *newNumerator[count + 1] = temp1;
    
    temp2 = *newDenominator[count];
    *newDenominator[count] = *newDenominator[count + 1];
    *newDenominator[count + 1] = temp2;
    

    需要:

    temp1 = newNumerator[count]; // Remove the *
    newNumerator[count] = newNumerator[count + 1];
    newNumerator[count + 1] = temp1;
    
    temp2 = newDenominator[count];
    newDenominator[count] = newDenominator[count + 1];
    newDenominator[count + 1] = temp2;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-06
      • 2021-06-15
      • 2011-04-23
      • 1970-01-01
      • 2017-02-24
      • 2013-10-06
      • 2016-07-26
      相关资源
      最近更新 更多