gpt4 book ai didi

c++ - 按特定值对结构成员进行排序

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:20:54 28 4
gpt4 key购买 nike

我遇到这个问题已经有一段时间了,所以我认为最好寻求指导。在没有帮助的情况下尝试搜索,现在已经为此苦苦挣扎了很长时间,所以我们开始吧。

我正在学习 C++,但在这个练习上遇到了困难:我必须使用 struct 保存 3 名学生的详细信息,然后按他们离学校的距离升序打印他们。第一个学生的详细信息由用户输入询问,其他学生的详细信息由变量初始化。一切顺利,直到我必须解决它们。

目前我正在尝试将一个数组传递给排序函数,其中包含我手动输入到该数组以进行测试的每个学生的详细信息。当我尝试直接从结构成员值初始化数组然后将该数组传递给排序函数时,我遇到了一些严重的问题。此外,在本练习中,我必须使用 C 数组。

有人可以告诉我正确的方法吗?也许有更简单的方法来做到这一点?提前致谢!这是我的代码:

编辑:平台:Windows 7,IDE:Visual Studio 2013

#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

struct personaldata {
char firstname[50], lastname[50], address[50], postal[50];
double distschool;
int shoesize;
};

struct SortRecordsAscending
{
bool operator()(const personaldata& lhs, const personaldata& rhs)
{
return (lhs.distschool < rhs.distschool);
}
};

void SortingObjectsExample(personaldata *details, int SIZE)
{
sort(details, details + SIZE, SortRecordsAscending());
for (int i = 0; i < SIZE; ++i)
{
cout << details[i].firstname << " " << details[i].lastname << endl;
cout << details[i].address << " " << details[i].postal << endl;
cout << "Distance from school: " << details[i].distschool << endl;
cout << "Shoe size: " << details[i].shoesize << endl;
cout << endl;
}
}

int main() {

personaldata student1, student2, student3;
const int SIZE(3); // Amount of students


//Student 1
cout << "Enter first name: ";
cin.getline(student1.firstname, 50);

cout << "Enter last name: ";
cin.getline(student1.lastname, 50);

cout << "Enter distance from school: ";
cin >> student1.distschool;
cin.ignore();

cout << "Enter address: ";
cin.getline(student1.address, 50);

cout << "Enter postal: ";
cin >> student1.postal;

cout << "Enter shoe size: ";
cin >> student1.shoesize;

//Student 2
strcpy_s(student2.firstname, 50, "Olli");
strcpy_s(student2.lastname, 50, "Oppilas");
student2.distschool = 9.4;
strcpy_s(student2.address, 50, "Opiskelijakatu 5a 14");
strcpy_s(student2.postal, 50, "40200");
student2.shoesize = 39;

//Student 3
strcpy_s(student3.firstname, 50, "Ossi");
strcpy_s(student3.lastname, 50, "Oppilas");
student3.distschool = 8.4;
strcpy_s(student3.address, 50, "Koululaiskatu 18b 2");
strcpy_s(student3.postal, 50, "40100");
student3.shoesize = 41;

personaldata details[3] = {
{ "Oiva", "Oppilas", "Koivukatu 8B 16", "40100", 9.7, 43 },
{ "Ossi", "Oppilas", "Koululaiskatu 18b 2", "40100", 8.4, 41 },
{ "Olli", "Oppilas", "Lehtitie 3B 15", "401200", 8.7, 38 }
};

SortingObjectsExample(details, SIZE);

}

上面的代码按照我想要的方式工作。但是,如果我这样做:

personaldata details[3] = {
{ student1.firstname, "Oppilas", "Koivukatu 8B 16", "40100", 9.7, 43 },
{ student2.firstname, "Oppilas", "Koululaiskatu 18b 2", "40100", 8.4, 41 },
{ student3.firstname, "Oppilas", "Lehtitie 3B 15", "401200", 8.7, 38 }
};

SortingObjectsExample(details, SIZE);

我从编译器那里得到

1>------ Build started: Project: Harj17, Configuration: Debug Win32 ------
1> harj17.cpp
1>harj17.cpp(80): error C2440: 'initializing' : cannot convert from 'char (*)[50]' to 'char'
1>There is no context in which this conversion is possible
1>harj17.cpp(80): warning C4244: 'initializing' : conversion from 'double' to 'char', possible loss of data
1>harj17.cpp(80): error C2440: 'initializing' : cannot convert from 'char [50]' to 'char'
1> There is no context in which this conversion is possible
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

如果像这样使用指针:

    personaldata details[3] = {
{ *student1.firstname, "Oppilas", "Koivukatu 8B 16", "40100", 9.7, 43 },
{ *student2.firstname, "Oppilas", "Koululaiskatu 18b 2", "40100", 8.4, 41 },
{ *student3.firstname, "Oppilas", "Lehtitie 3B 15", "401200", 8.7, 38 }
};

它会编译程序但有警告消息并且不能正常工作,它只打印人名的第一个字母。

    1>------ Build started: Project: Harj17, Configuration: Debug Win32 ------
1> harj17.cpp
1>harj17.cpp(80): warning C4244: 'initializing' : conversion from 'double' to 'char', possible loss of data
1> Harj17.vcxproj -> Harj17.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

这是运行程序时的输出。您可以看到名称有问题,其他信息也被搞砸了。

Enter first name: Example
Enter last name: Student
Enter distance from school: 13.37
Enter address: Example Address 6B 16
Enter postal: 1337
Enter shoe size: 43
EOppilas Koivukatu 8B 16
40100 +
Distance from school: 0
Shoe size: 0

OOppilas Koululaiskatu 18b 2
40100)
Distance from school: 0
Shoe size: 0

OOppilas Lehtitie 3B 15
401200&
Distance from school: 0
Shoe size: 0

最佳答案

奇怪的是你被允许使用STL算法而不是数据结构。从你的问题中我不清楚你有什么困难。排序结构或构建它们?无论如何,这是一种创建和排序结构的简单方法。由于您的排序函数不需要任何状态,因此它不需要是仿函数。它可以只是一个普通函数。

为了进一步改进这一点,我会使用 vector ,将流运算符添加到结构中并使用构造函数。

#include <algorithm>
using namespace std;

struct henkilotiedot { // Person first name, last name, address and postal
string etunimi, sukunimi, osoite, postinumero;
double koulumatka; // Distance from school
int kengannumero; // Shoe size
};

bool SortByDistance(const henkilotiedot& lhs, const henkilotiedot& rhs)
{
return lhs.koulumatka < rhs.koulumatka;
}

int main()
{
const int SIZE = 3;
henkilotiedot data[] = { { "blah", "blah", "blah", "blah", 5.0, 10 }, { "blah", "blah", "blah", "blah", 1.0, 10 }, { "blah", "blah", "blah", "blah", 15.0, 10 } };
sort(data, data + SIZE, SortByDistance);
}

关于c++ - 按特定值对结构成员进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26437819/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com