【发布时间】:2012-03-12 04:24:10
【问题描述】:
所以我尝试使用排序功能(类似于气泡)并将对象传递给它。如果该对象更大(按字母顺序),则 switch 然后返回 true 并在它之前切换它。尽管在mySort() 内的 if 语句中,我不断收到错误消息,上面写着“arr [j] 中的运算符 [] 不匹配”,但据我了解,我正在传递一个对象数组,对吗?为什么会发生这种情况,我该如何解决?
这是司机
#include <iostream>
#include <fstream>
#include <string>
#include "phoneEntry.h"
using namespace std;
void mySort(PhoneEntry &arr, int size)
{
bool inOrder = false;
string temp;
for (int i = size - 1; i > 0 && !inOrder; i--)
{
inOrder = true;
for (int j = 0; j < i; j++)
{
if(arr.alphaGreater(arr[j]))
{
inOrder = false;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
};
int main()
{
const int MAXNUM = 500;
PhoneEntry entry[MAXNUM];
ifstream filezilla;
filezilla.open("phone.txt");
int count = 0;
if(filezilla)
{
while(count < MAXNUM && entry[count].readEntry(filezilla))
{
count++;
mySort(entry[count], count);
}
for(int i = 0; i < count; i++)
{
entry[i].writeEntry(cout) << endl;
}
}
else
{
cout << "404" << endl;
}
return 0;
}
文本排序 (http://pastebin.com/HE8Rsmbg)
【问题讨论】:
-
不,您传递的是对单个
PhoneEntry对象的引用 -
entry[count]是单个PhoneEntry。函数mySort不按值获取(制作副本),而是通过引用获取它,这样效率更高,并且还允许您在需要时对其进行修改。
标签: c++ sorting loops bubble-sort