按姓氏对列表进行排序

Sorting a list by last name

本文关键字:排序 列表      更新时间:2023-10-16

Jason Chimera SE 7039990101

Mike Knuble SSE 7039990102

Karl Alzner JSE 7039990202

Tom Poti SE 7039990203

Alex Ovechkin EGM 7039990001

我正在尝试从文件中阅读上面的信息,然后按姓氏对其进行排序。我该如何解决此错误:

错误:预期a':'

对于我在员工信息类中声明的字符串?

 class EmployeeInformation {
  public string firstName;
  public string lastName;
  public string title;
  public string phonenumber;
EmployeeInformation(&firstName, &lastName, &title, &phonenumber)
{
    this->firstName = firstName;
    this->lastName = lastName;
    this->initials = title;
    this->id = phoneumber;
}
};

 #include <vector>
 #include <algorithm>
 #include <iostream>
 #include <fstream>
 using namespace std;
int main(int argc, char**argv) {
ifstream file;
file.open("employees.txt");
if (!file.is_open())
    cout << "Error opening file";
vector<EmployeeInformation> employees;
while (!file.eof())
{
    string firstName, lastName, title, phonenumber;
    file >> firstName >> lastName >> title >> phonenumber;
    EmployeeInformation person(firstName, lastName, title, phonenumber);
    employees.push_back(person);
    // end file
}
sort(employees.begin(), employees.end(), [](EmployeeInformation a, EmployeeInformation b) { return a.lastName < b.lastName });
for (EmployeeInformation i : employees)
    // print person info in sorted order
return 0;

}

很多小错误。

公共部分。

在C 公共中,其次是':'标记部分。另外,您需要包括string定义。居住在std::名称空间中。

  public: std::string firstName;
  public: std::string lastName;
  public: std::string title;
  public: std::string phonenumber;

C 是类型的非常学费。

您需要指定所有参数的类型:

EmployeeInformation(string const& firstName, string const & lastName, string const& title, string const& phonenumber)

未知名称:

this->initials = title;
  //  ^^^^^^^^   Not a member of this class. Did you mean title.
this->id = phoneumber;
  //  ^^         Not a member of this class. Did you mean phoneumber

语句由';''

终止
return a.lastName < b.lastName
                       //      ^^^^^   missing here.

将其修复的内容编译后。但是我将固定版本进行代码评论以进行更详细的分解。

当所有完整的主看起来像这样时:

int main(int argc, char**argv)
{
    std::ifstream file("employees.txt");
    // Read data into vector
    std::vector<EmployeeInformation> employees(std::istream_iterator<EmployeeInformation>(file),
                                               (std::istream_iterator<EmployeeInformation>()));
    // Sort container.
    std::sort(std::begin(employees), std::end(employees),
              [](EmployeeInformation const& lhs, EmployeeInformation const& rhs) { return lhs.lastName < rhs.lastName;});
    // Copy container to stream.
    std::copy(std::begin(employees), std::end(employees),
              std::ostream_iterator<EmployeeInformation>(std::cout));
}

您的课程未正确声明。例如,您无法从构造函数参数中省略数据类型。

您也没有正确读取文件。

尝试更多类似的东西:

#include <vector>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
class EmployeeInformation
{
public:
    string firstName;
    string lastName;
    string title;
    string phoneNumber;
    EmployeeInformation(const string &_firstName, const string &_lastName, const string &_title, const string &_phonenumber) :
        firstName(_firstName),
        lastName(_lastName),
        title(_title),
        phoneNumber(_phoneNumber)
    {
    }
};
int main(int argc, char**argv)
{
    ifstream file;
    file.open("employees.txt");
    if (!file.is_open())
    {
        cout << "Error opening file";
        return 0;
    }
    vector<EmployeeInformation> employees;
    string line;
    while (getline(file, line))
    {
        string firstName, lastName, title, phonenumber;
        istringstream iss(line);
        iss >> firstName >> lastName >> title >> phonenumber;
        EmployeeInformation person(firstName, lastName, title, phonenumber);
        employees.push_back(person);
    }
    sort(employees.begin(), employees.end(), [](const EmployeeInformation &a, const EmployeeInformation &b) { return a.lastName < b.lastName; });
    for (EmployeeInformation i : employees)
    {
        // print person info in sorted order
        cout << i.lastName << " " << i.firstName << " " << i.title << " " i.phoneNumber << endl;
    }
    return 0;
}

访问修饰符的语法不正确(您使用的是Java样式而不是C )。在C 中,您会声明这样的完全公开课程:

 class EmployeeInformation {
 public:
   string firstName;
   string lastName;
   string title;
   string phonenumber;
   //...
};

如果您确实想公开所有数据,则也可以使用结构而不是类,因为默认情况下结构的成员是公开的。

首先,您在类中的所有变量声明都是错误的。

 class EmployeeInformation {
public:
string firstName;
string lastName;
string title;
string phonenumber;
EmployeeInformation(string &firstName,string &lastName,string &title,string &phonenumber)
{
this->firstName = firstName;
this->lastName = lastName;
this->title = title;            //there is no variable named initial in class
this->phonenumber = phonenumber; //there is no variable named id in class
}
};

然后您缺少半分离。

sort(employees.begin(), employees.end(), [](EmployeeInformation a, EmployeeInformation b) { return a.lastName < b.lastName; });  //there should be a semicolon after return statement