重载运算符以允许 qsort 在C++中工作
Overload operators to allow qsort to work in C++
我用C++写了一段代码,然后决定将其更改为面向对象和除qsort工作之外的所有函数。我已经查找了它与运算符重载有关,但我不知道该怎么做。有人可以帮忙吗?
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
class OpAmps {
private:
char Name[20];
unsigned int PinCount;
double SlewRate;
public:
void Enter(OpAmps&, unsigned long&);
void Save(const OpAmps*, unsigned long);
void Load(OpAmps*, unsigned long&);
void Sort(OpAmps*, unsigned long);
int SortName(const void*, const void*);
int SortSlewRate(const void*, const void*);
void Display(const OpAmps*, unsigned long);
};
#define DATABASE_MAX 10
#define DATABASE_FILENAME "database.txt"
int main()
{
OpAmps OpAmp[DATABASE_MAX];
OpAmps Menu;
unsigned long database_length = 0;
char UserInput;
while (1) {
cout << endl;
cout << "Op-amp database menu" << endl;
cout << "--------------------" << endl;
cout << "1. Enter a new op-amp into the database" << endl;
cout << "2. Save the database to disk" << endl;
cout << "3. Load the database from disk" << endl;
cout << "4. Sort the database" << endl;
cout << "5. Display the database" << endl;
cout << "6. Exit from the program" << endl << endl;
cout << "Enter your option: ";
cin >> UserInput;
cout << endl;
switch(UserInput) {
case '1':
Menu.Enter(OpAmp[database_length], database_length);
break;
case '2':
Menu.Save(OpAmp, database_length);
break;
case '3':
Menu.Load(OpAmp, database_length);
break;
case '4':
Menu.Sort(OpAmp, database_length);
break;
case '5':
Menu.Display(OpAmp, database_length);
break;
case '6':
return 0;
default:
cout << "Invalid entry" << endl << endl;
break;
}
}
}
void OpAmps::Enter(OpAmps& Op, unsigned long& length)
{
if (length == DATABASE_MAX) {
cout << "The database is full" << endl;
}
else {
cout << "Add new data" << endl;
cout << "------------" << endl;
cout << "Enter op-amp name: ";
cin >> Op.Name;
cout << "Enter number of pins: ";
cin >> Op.PinCount;
cout << "Enter slew rate: ";
cin >> Op.SlewRate;
cout << endl;
length++;
}
}
void OpAmps::Save(const OpAmps* Op, unsigned long length)
{
fstream output_file;
output_file.open(DATABASE_FILENAME, ios::out);
if(output_file.good()) {
output_file << length << endl << endl;
for (unsigned long i=0;i<length;i++) {
output_file << Op[i].Name << endl;
output_file << Op[i].PinCount << endl;
output_file << Op[i].SlewRate << endl << endl;
}
}
output_file.close();
}
void OpAmps::Load(OpAmps* Op, unsigned long& length)
{
fstream input_file;
input_file.open(DATABASE_FILENAME, ios::in);
if(input_file.good()) {
input_file >> length;
for (unsigned long i=0;i<length;i++) {
input_file >> Op[i].Name;
input_file >> Op[i].PinCount;
input_file >> Op[i].SlewRate;
}
}
input_file.close();
}
void OpAmps::Sort(OpAmps* Op, unsigned long length)
{
char UserInput;
cout << endl;
cout << "Sorting options" << endl;
cout << "---------------" << endl;
cout << "1. To sort by name" << endl;
cout << "2. To sort by slew rate" << endl;
cout << "3. No sorting" << endl << endl;
cout << "Enter your option: ";
cin >> UserInput;
cout << endl;
switch(UserInput) {
case '1':
cout<<"sortName"<<endl;
qsort(Op,length,sizeof(OpAmps),SortName);
break;
case '2':
cout<<"sortslew"<<endl;
qsort(Op,length,sizeof(OpAmps),SortSlewRate);
break;
case '3':
return;
default:
cout << "Invalid entry" << endl << endl;
break;
}
}
int SortName(const void *First, const void* Second)
{
return strcmp(((OpAmps *) First)->Name, ((OpAmps *) Second)->Name);
}
int SortSlewRate (const void *First, const void* Second)
{
return (int) ((((OpAmps *) First)->SlewRate > ((OpAmps *) Second)->SlewRate)? 1 : -1);
}
void OpAmps::Display(const OpAmps* Op, unsigned long length)
{
if (length == 0) {
cout << "No elements in the database" << endl;
}
else {
cout << endl;
for (unsigned long i=0;i<length;i++) {
cout << "Name: " << Op[i].Name <<endl;
cout << "Number of Pins: " << Op[i].PinCount << endl;
cout << "Slew Rate: " << Op[i].SlewRate << endl;
cout << endl;
}
}
}
干杯家伙:Dxx
我认为在这种情况下您的问题是您定义了具有相同名称的不同种类函数。 您已经对 SortName 和 SortSlewRate 执行此操作。
一个定义说"SortName"和"SortSlewRate"是OpAmps类的成员函数,但是,在你的代码中,SortName和SortSlewRate是全局函数。
qsort 是一个 C 函数,需要函数指针而不是成员函数指针。
由于您的函数旨在成为全局函数,但访问 OpAmps 类的私有成员,因此您应该将"friend"关键字放在它们前面。
尝试将您的声明更改为此...
class OpAmps {
private:
char Name[20];
unsigned int PinCount;
double SlewRate;
public:
void Enter(OpAmps&, unsigned long&);
void Save(const OpAmps*, unsigned long);
void Load(OpAmps*, unsigned long&);
void Sort(OpAmps*, unsigned long);
void Display(const OpAmps*, unsigned long);
friend int SortName(const void*, const void*);
friend int SortSlewRate(const void*, const void*);
};
综上所述,你最好使用 std::sort 而不是 qsort,你会得到类型安全的优势,在某些情况下,由于编译器可以为你做出的优化,性能甚至更高。
您的定义如下所示
class OpAmps {
private:
char Name[20];
unsigned int PinCount;
double SlewRate;
public:
void Enter(OpAmps&, unsigned long&);
void Save(const OpAmps*, unsigned long);
void Load(OpAmps*, unsigned long&);
void Sort(OpAmps*, unsigned long);
void Display(const OpAmps*, unsigned long);
friend bool SortName(const OpAmps &, const OpAmps &);
friend bool SortSlewRate(const OpAmps &, const OpAmps &);
};
你会像这样使用"排序"...
sort(Op, Op + length,SortName);
和
sort(Op,Op + length,SortSlewRate);
请注意,排序略有不同,函数返回 true(小于)或 false(等于或大于)而不是 -1(小于)、0(等于)、-1(大于),并且它们不是传递的指针,而是传递的引用。
你会这样定义它们。
bool SortName(const OpAmps &First, const OpAmps &Second)
{
return strcmp(First.Name, Second.Name) < 0;
}
bool SortSlewRate (const OpAmps &First, const OpAmps &Second)
{
return First.SlewRate < Second.SlewRate;
}
最后,为了更C++喜欢,您可以切换您的
char Name[20];
自
string Name;
这将简化您的 SortName 函数,并提供针对令人讨厌的缓冲区溢出错误的安全性。
bool SortName(const OpAmps &First, const OpAmps &Second)
{
return First.Name < Second.Name;
}
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 链表c++插入,所有情况都已检查,但没有任何工作
- 为什么stream::忽略未按预期工作