【问题标题】:C++ Sorting vector of structs alphabeticallyC++ 按字母顺序对结构的向量进行排序
【发布时间】: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 &lt;algorithm&gt;
  • 谢谢,而且我没有把void放在sortEmail(vector&lt;Record&gt;&amp; recordList)前面...'facepalm'

标签: c++ sorting vector struct


【解决方案1】:

我想你忘了包括&lt;algorithm&gt;
添加#include &lt;algorithm&gt;,看看是否有效。

【讨论】:

  • 是的,也忘了void前面的sortEmail(vector&lt;Record&gt;&amp; recordList)...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-12
  • 1970-01-01
  • 1970-01-01
  • 2018-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多