// 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 ©This)
{
nameOfstartingCity = copyThis.nameOfstartingCity;
nameOfendingCity = copyThis.nameOfendingCity;
return *this;
}
答案 0 :(得分:0)
将Leg
修改为
getDistance
没有必要将变量传递给getter或执行任何swapperoo废话。totalDistance
制作此const
毫无意义。它使赋值运算符无法使swap
和sort
成为不可能。新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]);
这使代码工作。现在让它正常工作。
Leg
<<
运算符新腿
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;
};
然后
std::sort
替换天真排序。为什么重新发明轮子?for
循环,因此我们不需要任何幻数。<<
运算符新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;
}