结构体sort自定义排序
时间: 2023-11-02 16:02:30 浏览: 138
结构体排序中的自定义排序可以通过自定义比较函数来实现。比较函数需要返回一个bool类型的值,根据返回值来确定排序的顺序。在比较函数中,可以根据结构体中的特定成员变量来进行排序。以下是一个使用结构体sort进行自定义排序的示例代码:
```cpp
#include <algorithm>
using namespace std;
struct clever {
int x;
int y;
}a[105];
bool cmp(clever a, clever b) {
return a.y < b.y; // 以y为比较参数呈升序排列
}
int main() {
int n; // 数组大小
sort(a, a + n, cmp); // 使用自定义的比较函数进行排序
}
```
在上述代码中,cmp函数是一个自定义的比较函数,根据结构体中的y成员变量进行升序排序。在main函数中,使用sort函数对结构体数组a进行排序,排序结果将根据cmp函数的返回值确定。
相关问题
C++中结构体的自定义sort排序
### C++ 中对结构体进行自定义 `sort` 排序
在 C++ 的 STL 中,可以通过重载运算符或者提供比较函数的方式实现对结构体的排序。以下是详细的说明以及示例代码。
#### 方法一:通过重载 `<` 运算符
如果希望直接使用默认的 `std::sort()` 函数来排序,则可以在结构体内重载小于 (`<`) 运算符。这种方式适用于简单的排序逻辑[^5]。
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Man {
int g; // 工号
int age; // 年龄
string name; // 姓名
// 重载 '<' 运算符
bool operator<(const Man& other) const {
if (age != other.age) return age < other.age; // 按年龄升序
if (g != other.g) return g < other.g; // 如果年龄相同,按工号升序
return name < other.name; // 如果前两者都相同,按姓名字典序升序
}
};
int main() {
vector<Man> people = {{101, 30, "Alice"}, {102, 25, "Bob"}, {103, 30, "Charlie"}};
sort(people.begin(), people.end()); // 使用默认排序
for (auto& person : people) {
cout << person.g << " " << person.age << " " << person.name << endl;
}
return 0;
}
```
此方法的优点在于简洁明了,适合于单一字段或多字段组合的简单排序需求。
---
#### 方法二:通过外部比较函数
当需要更复杂的排序规则时,可以编写一个独立的布尔型比较函数并将其作为第三个参数传递给 `std::sort()` 函数[^2]。
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
string name;
int id;
};
// 定义比较函数
bool cmp(const Student& a, const Student& b) {
if (a.id != b.id) return a.id < b.id; // 按 ID 升序
return a.name < b.name; // 若 ID 相同则按名字字典序升序
}
int main() {
vector<Student> students = {{"Alice", 3}, {"Bob", 1}, {"Charlie", 2}};
sort(students.begin(), students.end(), cmp); // 使用自定义比较函数
for (auto& student : students) {
cout << student.name << " " << student.id << endl;
}
return 0;
}
```
这种方法更加灵活,允许开发者自由设计任意复杂度的排序条件。
---
#### 方法三:Lambda 表达式简化比较逻辑
现代 C++ 提供了 Lambda 表达式的功能,使得我们可以将比较逻辑嵌入到调用现场而无需单独声明函数[^4]。
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Person {
int score;
int age;
string name;
};
int main() {
vector<Person> persons = {{85, 20, "Tom"}, {90, 22, "Jerry"}, {85, 21, "Spike"}};
sort(persons.begin(), persons.end(), [&](const Person& a, const Person& b) -> bool {
if (a.score != b.score) return a.score > b.score; // 成绩降序
if (a.age != b.age) return a.age > b.age; // 同分情况下按年龄降序
return a.name < b.name; // 再次相同则按名字字典序升序
});
for (auto& p : persons) {
cout << p.name << " " << p.score << " " << p.age << endl;
}
return 0;
}
```
利用 Lambda 表达式不仅保持了代码的一致性和可读性,还减少了额外命名带来的负担。
---
### 总结
以上三种方式分别展示了如何基于不同场景选择合适的策略完成对结构体数组或容器的排序操作。无论是内置的 `<` 运算符还是显式的比较器亦或是现代化的 Lambda 技术都可以满足实际开发中的多样化需求[^1][^3].
c++结构体和自定义函数排序
在C++中,结构体(struct)是一种用户自定义的数据类型,用于组合不同类型的数据成员,形成一个新的数据实体。若想对结构体数组或结构体变量按照某个成员进行排序,可以借助标准模板库(STL)提供的算法,比如`std::sort()`。
自定义函数排序通常涉及提供一个比较函数(comparison function),该函数接受两个结构体实例作为输入,并返回一个布尔值表示它们的相对顺序。例如:
```cpp
#include <algorithm>
#include <iostream>
// 定义结构体
struct Person {
std::string name;
int age;
// 按照年龄升序的默认比较函数
bool operator<(const Person& other) const {
return age < other.age;
}
};
// 自定义函数排序示例
void sortPersonByAge(std::vector<Person>& people) {
std::sort(people.begin(), people.end());
}
int main() {
std::vector<Person> people = {{"Alice", 25}, {"Bob", 30}, {"Charlie", 20}};
sortPersonByAge(people);
for (const auto& person : people) {
std::cout << "Name: " << person.name << ", Age: " << person.age << std::endl;
}
return 0;
}
```
在这个例子中,我们通过`operator<`重载了小于运算符,使得结构体`Person`可以直接用于`std::sort()`进行按年龄升序排序。如果你想降序排列,只需将`age < other.age`改为`age > other.age`即可。
阅读全文
相关推荐













