gpt4 book ai didi

C++ 为什么我的交换函数没有被调用?

转载 作者:行者123 更新时间:2023-12-05 08:48:06 27 4
gpt4 key购买 nike

概述:我正在尝试按距离对这些数组对象进行排序,然后按从最短到最长的顺序将距离打印到控制台。我正在使用 Leg 类将字符串存储为字符指针并将距离存储为 double 。我已经重载了 < 运算符,但我有一个正在使用交换的排序,但我的构建失败了。这是我第一次使用重载运算符,所以我对它的工作原理还很陌生。我觉得我正朝着正确的方向前进。对于任何反馈,我们都表示感谢。这是代码:

#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>
using namespace std;

class Leg {

const char* const startingCity;
const char* const endingCity;
const double distance;

public:
Leg(const char* const, const char* const, const double);
bool operator<(const Leg&); //for swap
double getDistance() const {return distance;};
void output(ostream&) const;
};

int main () {

Leg a[] =
{
Leg("Antioch", "Brentwood", 6.9),
Leg("Pittsburg", "Elk Grove", 40.1),
Leg("Benicia", "Martinez", 2.9),
Leg("Napa", "Fairfield", 13.3),
Leg("Brentwood", "Novato", 48.3),
Leg("Fremont", "Concord", 29.8),
Leg("San Pablo", "San Rafael", 11.8),
Leg("Walnut Creek", "Dixon", 39.7),
Leg("Santa Rosa", "Napa", 26.4),
Leg("Daly City", "Alameda", 20.2),
Leg("San Leandro", "Dublin", 20.3),
Leg("Modesto", "Antioch", 52.5),
Leg("Pleasanton", "San Ramon", 10.6),
Leg("Lafayette", "Livermore", 25.4),
Leg("Vacaville", "San Francisco", 77.2),
Leg("Hayward", "Danville", 11.9),
Leg("Berkeley", "Richmond", 10.1),
Leg("Oakland", "Oakley", 33.3),
Leg("Lodi", "Mountain View", 66.2),
Leg("Antioch", "San Jose", 76.9),
Leg("Milpitas", "Pacifica", 54.9),
Leg("Burlingame", "Petaluma", 73.8),
Leg("Redwood City", "San Bruno", 19.2),
Leg("San Mateo", "Brentwood", 42.6),
Leg("Union City", "Ripon", 83.0 ),
Leg("Sunnyvale", "Tracy", 41.7),
Leg("Rohnert Park", "Benicia", 36.9),
Leg("Foster City", "Pittsburg", 6.9),
Leg("Danville", "Manteca", 43.2),
Leg("Antioch", "Stockton", 28.4),
Leg("San Francisco", "Oakland", 9.1),
Leg("Antioch", "Walnut Creek", 15.8),
Leg("Vallejo", "Berkeley", 16.2),
Leg("San Ramon", "Walnut Creek", 9.9),
Leg("Tracy", "Fremont", 33.6),
Leg("Martinez", "Concord", 6.7),
Leg("Lafayette", "Daly City", 22.8),
Leg("Livermore", "Petaluma", 60.6),
Leg("Galt", "Mountain View",72.5),
Leg("Pleasant Hill", "Richmond", 15.8)

};

const int SIZE = (sizeof(a) / sizeof(a[0]));

for(int i = 0; i < SIZE; i++)
for(int j = i + 1; j < SIZE; j++)
if(a[j].getDistance() < a[i].getDistance())
swap(a[i], a[j]); //error is occurring on this line

for(int i = 0; i < SIZE; i++)
{
a[i].output(cout);
}

return 0;
}

Leg::Leg(const char* const start, const char* const end, const double dist) //constructor
:startingCity(start), endingCity(end), distance(dist)
{

}

void Leg::output(ostream& out) const //output function
{
out << "Leg: " << startingCity << " to " << endingCity << ", " << distance << " miles." << endl;
}

bool Leg::operator<(const Leg& rhs)
{
return distance < rhs.getDistance();
}

最佳答案

你的 Leg由于const,类既不可移动构造也不可移动分配成员。所以不可能是swapped .

删除 const其成员的要求,它将获得隐式移动构造函数和赋值运算符和 std::swap会把它们捡起来。

class Leg {
const char* startingCity;
const char* endingCity;
double distance;
. . .

最后的注释:operator <swap 没有影响.

关于C++ 为什么我的交换函数没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66483094/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com