冒泡排序不适用于对 C++ 中的动态对象数组进行排序
Posted
技术标签:
【中文标题】冒泡排序不适用于对 C++ 中的动态对象数组进行排序【英文标题】:Bubble sorting doesn't work to sort dynamic array of objects in C++ 【发布时间】:2017-12-08 20:04:14 【问题描述】:我的 City 类具有以下私有数据:城市名称(char 类型)、宽度(double)、长度(double)和高度(double)。我必须制作动态数组,默认情况下由构造函数插入 - City(),当程序启动时。然后程序使用方法 output() 并打印插入的城市数组。mA 是我的城市对象。我应该使用气泡排序以按城市的长度对城市进行排序。我有复制构造函数,运算符 = 并且我必须将 double max(这是变量,在冒泡排序中用于存储当前最大值)转换为 City 类型,为此我使用了带有一个的构造函数参数:City(double max)。问题是排序不起作用。我认为问题出在我对带有一个参数的构造函数的定义中(将 double 类型转换为 city 类型)。
#include "stdafx.h"
#include<iostream>
#include<math.h>
#include <algorithm>
using namespace std;
class City
private: char *name;
double width;
double length;
double height;
public:
void Output();
City();
~City();
City(double max)
name = "";
width = 0;
length = max;
height = 0;
double GetLength()
return length;
double GetWidth() return width;
double GetHeight() return height;
char GetName() return *name;
City(const City& that)
name = new char[strlen(that.name) + 1];
for (int i = 0; i <= strlen(that.name); i++)
name[i] = that.name[i];
//strcpy(name, that.name);
width = that.width;
length = that.length;
height = that.height;
City& operator=(const City that)
name = that.name;
width = that.width;
length = that.length;
height = that.height;
return*this;
;
City::City()
char ime[20];
cout << "Name= ";
cin >> ime;
name = new char[strlen(ime) + 1];
for (int i = 0; i <= strlen(ime); i++)
name[i] = ime[i];
cout << "Width= ";
cin >> width;
cout << "Length= ";
cin >> length;
cout << "Height= ";
cin >> height;
void City::Output()
cout << "Name is: " << name << endl;
cout << " Width is: " << width << " deg" << endl;;
cout << " Length is: " << length << " deg" << endl;
cout << " Height is: " << height << " m" << endl;
return;
City::~City()
cout << " " << endl;
cout << "Destructor of City!" << endl;
delete[] name;
int main()
int n;
City *mA;
cout << "Input number of cities: " << endl;
cin >> n;
mA = new City[n];
for (int j = 0; j < n; j++)
mA[j].Output();
cout << "Cities from west to east, sorted by their length" << endl;
double max = mA[0].GetLength();
for (int j = 1; j<n; j++)
if (mA[j - 1].GetLength()>mA[j].GetLength())
max = mA[j - 1].GetLength();
mA[j - 1] = mA[j];
mA[j] = max;
for (int j = 0; j < n; j++)
mA[j].Output();
delete[]mA;
return 0;
【问题讨论】:
“Doesn't work”不是错误描述,您希望它做什么以及它会做什么? 冒泡排序有两个嵌套循环。 您应该使用std::swap
交换值。现在您的mA[j] = max;
每次都会从max
创建一个新对象(使用您怀疑的构造函数)。如果您不想要隐式转换,可以使用 explicit City(double max)
。
name = "";
为常量数据分配一个非常量指针。对此要非常小心。例如,想想当这个对象被销毁并运行delete[] name;
时会发生什么。考虑改为设置name = nullptr;
。如果您尝试滥用 name
,您将收到更多有用的错误消息。
使用 std::string,而不是 char 数组。
【参考方案1】:
City::City()
char ime[20];
cout << "Name= ";
cin >> ime;
name = new char[strlen(ime) + 1];
for (int i = 0; i <= strlen(ime); i++)
name[i] = ime[i];
...
首先你必须修复构造函数。虽然不一定是错的,但你不应该在构造函数中要求输入。改为为Input()
添加一个单独的函数
City& operator=(const City that)
name = that.name;
...
return*this;
这个赋值运算符是错误的。 name
是一个指针,您不想分配指针(不在这种情况下)。您应该使用与以前相同的方法来复制名称:
name = new char[strlen(ime) + 1];
strcpy(name, ime);
冒泡排序应该像下面的方法一样完成。另外,您添加了#included <string>
。你应该改用std::string
!
#include <iostream>
#include <string>
using namespace std;
class City
private:
char *name;
double width;
double length;
double height;
public:
City()
name = nullptr;
width = 0;
length = 0;
height = 0;
City(const City& that)
name = new char[strlen(that.name) + 1];
strcpy(name, that.name);
width = that.width;
length = that.length;
height = that.height;
City& operator=(const City that)
name = new char[strlen(that.name) + 1];
strcpy(name, that.name);
width = that.width;
length = that.length;
height = that.height;
return*this;
~City()
delete[] name;
void Input()
char buffer[100];
cout << "Name= ";
cin >> buffer;
name = new char[strlen(buffer) + 1];
strcpy(name, buffer);
cout << "Width= ";
cin >> width;
cout << "Length= ";
cin >> length;
cout << "Height= ";
cin >> height;
void Output()
cout << "Name is: " << name << ", " << length << endl;
cout << " Width is: " << width << " deg" << endl;
cout << " Length is: " << length << " deg" << endl;
cout << " Height is: " << height << " m" << endl << endl;
double GetLength() return length;
double GetWidth() return width;
double GetHeight() return height;
char GetName() return *name;
;
int main()
int n;
City *mA;
cout << "Input number of cities: " << endl;
cin >> n;
City *mA = new City[n];
//read input
for(int j = 0; j < n; j++)
mA[j].Input();
//bubble sort:
for(int i = 0; i < n; i++)
for(int j = i + 1; j < n; j++)
if(mA[i].GetLength() > mA[j].GetLength())
//swap values:
City temp = mA[i];
mA[i] = mA[j];
mA[j] = temp;
for(int j = 0; j < n; j++)
mA[j].Output();
delete[]mA;
system("pause");
return 0;
【讨论】:
【参考方案2】:手动排序需要比较重新排序的条目 - - - - 评论 - - - - - for (int j = 1; jmA[j].GetLength()) ... 交换元素 ... j-=2; //指数 ...如果(j
如果将使用 std::sort 条件是否可靠由 STL 检查。 并且只会检查回调函数的比较条件。
问候,休伯特·赫曼努茨
【讨论】:
以上是关于冒泡排序不适用于对 C++ 中的动态对象数组进行排序的主要内容,如果未能解决你的问题,请参考以下文章