按姓氏对列表进行排序
Sorting a list by last name
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
相关文章:
- C 中的排序列表
- 使用霍尔分区方案的快速排序算法返回原始未排序列表
- 带有额外参数的排序列表以进行比较功能
- 算法,用于查找获取排序列表所需的最小移动元素到结束步骤
- 迭代排序列表并计算不同的数字
- 维护多集中数字的排序列表和另一个集合中的累积总和
- 超过合并 k 排序列表的时间限制(leetcode)
- 正在检查排序列表中是否有一个范围内的数字
- O(N) 查找,但在排序列表上使用 upper_bound 时进行 O(log(N)) 比较
- 如何在只更改一个元素的情况下在排序列表中进行快速排序
- 返回指向排序列表的指针;C++中的链表
- 除了显示平均,最高和最低温度之外,有没有办法打印出整个排序列表
- 整数排序列表..TMP
- c++排序:具有相同数组值的排序列表
- 复制C++中未排序列表的构造函数
- 如何将元素添加到排序列表中..?(C++)
- 三维对象的Z排序列表
- 在我的排序列表中插入项目时出现问题
- 将节点插入到不带头指针的排序列表中
- 使用"pop_front"从排序列表中删除元素会导致崩溃