使用赋值运算符功能按距离对数组对象进行排序



//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 << "nnnn" << 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;
}

Leg修改为

  1. 修复getDistance将变量传递给getter或做任何交换废话是没有意义的。
  2. 修复totalDistance没有必要进行此const。它使赋值运算符变得不可能,这使得swapsort不可能。
  3. 删除损坏的赋值运算符

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. 将<运算符添加到Leg>
  2. 输出例程替换为标准<<运算符
  3. 设置格式,使其更易于阅读

新腿

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. 摆脱所有数组大小的废话,因为幻数构建糟糕程序的方式比我数不清的要多。
  2. 将朴素排序替换为std::sort。为什么要重新发明轮子?
  3. 使用基于范围的for循环,这样我们就不需要任何幻数。
  4. 使用新的<<运算符

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 << "nnnn" << endl;
return 0;
}

最新更新