使用赋值运算符函数按距离排序数组对象

时间:2017-03-23 21:15:13

标签: c++

// lab5.cpp:定义控制台应用程序的入口点。     //如何根据距离对数组对象进行排序?我不太清楚如何使用赋值运算符函数......任何帮助都会得到集会赞赏..

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;
class Leg {
public:
    Leg(const char *nameS, const char *nameE,const double totalDis): nameOfstartingCity(nameS), nameOfendingCity(nameE), totalDistance(totalDis) {};
    double getDistance(double);
    void  outPut(ostream &);
    Leg &operator=(const Leg&);

private:
    const char *nameOfstartingCity;
    const char *nameOfendingCity;
    const double totalDistance;
};

int main()
{
    int arraySize = 0;
    const int SIZEOFARRAY = 11;
    Leg myArray[SIZEOFARRAY] = {    Leg("Pinole", "Richmond", 3),
                                    Leg("Redwood City", "Palo Alto", 21.6),
                                    Leg("Berkeley", "Oakland", 4.3),
                                    Leg("Concord", "Walnet Creek", 8.3),
                                    Leg("San Francisco", "Oakland", 23),
                            Leg("South San Francisco", "San Mateo",15.3),
                                    Leg("Palo Alto", "Sunnyvale", 11.2),
                                    Leg("San Jose", "Milpitas", 15.2) ,
                                    Leg("Fremont", "Union City", 16.3) ,
                                    Leg("San Leandro", "Piedmont", 19.4),
                                    Leg("Pinole", "San Jose", 40.1) };


    arraySize = sizeof(myArray) / sizeof(myArray[0]);

    for (int x = 0; x < arraySize; x++) {
        for (int j = x + 1; j < arraySize; j++)
            if (myArray[j].getDistance < myArray[x].getDistance)
                swap(myArray[x], myArray[j]);

    }

    for (int i = 0; i < arraySize; i++)
    {
        myArray[i].outPut(cout);
    }

    cout << "\n\n\n\n" << endl;

    system("PAUSE");
    return 0;
}
double Leg::getDistance(double x)
{
    x = totalDistance;
    cout << x << "   From getDistance " << endl;

     return x;
}

void Leg::outPut(ostream &x)
{
        x << "Leg : " << nameOfstartingCity << " to " << nameOfendingCity << 
         ", " << totalDistance << " miles." << endl;
}
Leg& Leg::operator=(const Leg &copyThis)
{
    nameOfstartingCity = copyThis.nameOfstartingCity;
    nameOfendingCity = copyThis.nameOfendingCity;
    return *this;
}

1 个答案:

答案 0 :(得分:0)

Leg修改为

  1. 修复getDistance没有必要将变量传递给getter或执行任何swapperoo废话。
  2. 修复totalDistance制作此const毫无意义。它使赋值运算符无法使swapsort成为不可能。
  3. 删除损坏的赋值运算符
  4. Leg

    class Leg {
    public:
        Leg(const char *nameS, const char *nameE,const double totalDis): nameOfstartingCity(nameS), nameOfendingCity(nameE), totalDistance(totalDis) {};
        double getDistance()
        {       
            return totalDistance;
        }
        void  outPut(ostream &);
    
    private:
        const char *nameOfstartingCity;
        const char *nameOfendingCity;
        double totalDistance;
    };
    

    然后正确调用修订后的getDistance函数。无论函数是否有参数,你总是需要在函数调用中使用括号。

    for (int x = 0; x < arraySize; x++) {
         for (int j = x + 1; j < arraySize; j++)
             if (myArray[j].getDistance() < myArray[x].getDistance())
                 swap(myArray[x], myArray[j]);
    

    这使代码工作。现在让它正常工作。

    1. 添加&lt;运营商Leg
    2. 使用标准<<运算符
    3. 替换输出例程
    4. 对其进行格式化,以便于阅读
    5. 新腿

      class Leg
      {
      public:
          Leg(const char *nameS,
              const char *nameE,
              const double totalDis) :
                  nameOfstartingCity(nameS),
                  nameOfendingCity(nameE),
                  totalDistance(totalDis)
          {
          }
          double getDistance()
          {
              return totalDistance;
          }
          friend ostream & operator<<(ostream &x, const Leg &leg)
          {
              x << "Leg : " << leg.nameOfstartingCity << " to " << leg.nameOfendingCity
                      << ", " << leg.totalDistance << " miles.";
              return x;
          }
      
          bool operator<(const Leg & rhs) const
          {
              return totalDistance < rhs.totalDistance;
          }
      
      private:
          const char *nameOfstartingCity;
          const char *nameOfendingCity;
          double totalDistance;
      };
      

      然后

      1. 摆脱所有数组大小的废话,因为magic numbers构建错误的程序的方式比我可以计算的多。
      2. std::sort替换天真排序。为什么重新发明轮子?
      3. 使用基于范围的for循环,因此我们不需要任何幻数。
      4. 使用新的<<运算符
      5. main

        int main()
        {
            Leg myArray[] =
            { 
              Leg("Pinole", "Richmond", 3),
              Leg("Redwood City", "Palo Alto", 21.6),
              Leg("Berkeley", "Oakland", 4.3),
              Leg("Concord", "Walnet Creek", 8.3),
              Leg("San Francisco", "Oakland", 23),
              Leg("South San Francisco", "San Mateo", 15.3),
              Leg("Palo Alto", "Sunnyvale", 11.2),
              Leg("San Jose", "Milpitas", 15.2),
              Leg("Fremont", "Union City", 16.3),
              Leg("San Leandro", "Piedmont", 19.4),
              Leg("Pinole", "San Jose", 40.1) 
            };
        
            std::sort(std::begin(myArray), std::end(myArray));
        
            for (Leg &leg : myArray)
            {
                cout << leg << '\n';
            }
        
            cout << "\n\n\n\n" << endl;
        
            return 0;
        }