【发布时间】:2016-11-26 23:30:22
【问题描述】:
我正在为入门编程课程构建这个记录管理程序。部分代码是函数sortEmail,它应该按字母顺序对向量记录列表进行排序并调用sortAlphabet 函数。我在这一行收到一个错误:sort(recordList.begin(), recordList.end(), compareAlphabet);“未在此范围内声明排序”。我该如何解决这个问题?
#include<iostream>
#include<vector>
#include<string>
using namespace std;
struct Record
{
string name;
string email;
};
bool compareAlphabet(const Record& a, const Record& b)
{
return a.name < b.name;
}
sortEmail(vector<Record>& recordList)
{
system("cls");
sort(recordList.begin(), recordList.end(), compareAlphabet);
cout<<"Name have been sorted alphabetically"<<endl;
for (int i=0; i!=recordList.size(); i++)
{
cout<<recordList[i].name<<endl;
cout<<recordList[i].email<<endl;
}
system("PAUSE");
system("cls");
}
【问题讨论】:
-
您能否将代码调整为复制问题所需的内容?
-
我建议阅读一些有关您尝试调用的
sort函数的文档。并将您的代码减少到minimal reproducible example。 -
我会这样做的!代码也被精简了:)
-
添加
#include <algorithm> -
谢谢,而且我没有把
void放在sortEmail(vector<Record>& recordList)前面...'facepalm'