Joh*_*ith 0 c++ sorting loops bubble-sort
所以我正在尝试使用排序功能(类似于气泡)并将其传递给它.如果该对象更大(按字母顺序),则切换然后返回true并将其与之前切换.虽然我在if语句里面写了mySort()"在arr [j]中没有匹配operator []"但我仍然收到错误但是根据我的理解我正在传递一个对象数组吗?为什么会发生这种情况,我该如何解决?
这是司机
#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;
}
Run Code Online (Sandbox Code Playgroud)
排序文本(http://pastebin.com/HE8Rsmbg)
arr 应该是一个数组,而不是像这样的引用 PhoneEntry arr[]
您应该将整个数组传递给排序,而不是单个元素,如下所示: mySort(entry, count);
除此之外,您的代码显示正常.
我应该补充一点,这不是C++-ish解决方案:在C++中管理数组的首选方法是使用std::vector<T>标准库中的容器.向量的好处是你不需要"在侧面"传递它们的大小.