C++set容器,自定义排序方法(使用仿函数(Functor)或者普通函数指针)

set容器自定义排序方法:仿函数或普通函数指针

在使用STL中的set容器时,如果需要自定义排序规则,可以使用仿函数(Functor)或者普通函数指针。

使用普通函数指针时,需要定义一个比较函数,该函数接受两个参数,返回一个bool类型的值,表示两个参数的大小关系。在创建set容器时,将比较函数的函数指针作为第二个参数传递给set,从而实现按照自定义规则进行排序。

例如,如果我们需要对一个存储了学生信息的结构体按照学生的年龄进行排序,可以定义一个比较函数AgeLess,如下所示:

struct Student {
    string name;
    int age;
};

bool AgeLess(const Student& s1, const Student& s2) {
    return s1.age < s2.age;
}

在上面的代码中,AgeLess是一个比较函数,它接受两个参数s1和s2,返回一个bool类型的值,表示s1和s2的大小关系。

当我们创建set容器时,可以将AgeLess作为第二个参数传递给set,从而实现按照学生年龄进行排序:

set<Student, bool(*)(const Student&, const Student&)> studentSet(AgeLess);

在上面的代码中,我们创建了一个set容器studentSet,它存储了Student类型的元素,并使用AgeLess作为比较函数,从而实现按照学生年龄进行排序。需要注意的是,我们需要将AgeLess的函数指针作为第二个参数传递给set,而不是直接传递AgeLess函数名。

总之,使用普通函数指针和仿函数都可以实现自定义排序规则,具体使用哪种方式取决于个人喜好和实际情况。

代码示例1(普通内置数据类型的排序)

函数指针方法

using namespace std;
#include <iostream>

#include <set>

bool MyCompareFunc(int v1, int v2)
{
	return v1 > v2;
}

void test01()
{
	// 指定排序规则
	set<int, bool (*)(int, int)> s2(MyCompareFunc); // 函数指针方法(参数名可省略)

	s2.insert(10);
	s2.insert(40);
	s2.insert(20);
	s2.insert(30);
	s2.insert(50);

	for (set<int, bool (*)(int, int)>::iterator it = s2.begin(); it != s2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

int main()
{

	test01();

	system("pause");

	return 0;
}

运行结果:

50 40 30 20 10 

仿函数方法

#include <iostream>
#include <set>
using namespace std;

class MyCompare {
public:
    bool operator()(int v1, int v2) const {  // 添加 const 提高安全性
        return v1 > v2;
    }
};

void test01() {
    set<int, MyCompare> s2;

    s2.insert(10);
    s2.insert(40);
    s2.insert(20);
    s2.insert(30);
    s2.insert(50);

    // 使用 range-based for loop,更现代和简洁
    for (auto num : s2) {
        cout << num << " ";
    }
    cout << endl;
}

int main() {
    test01();

    // 使用更跨平台的方式暂停程序,替换 system("pause")
    cout << "Press Enter to continue..." << flush;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    return 0;
}

运行结果:

50 40 30 20 10 

代码示例2(自定义对象的排序)

函数指针方法

#include <iostream>
#include <set>
using namespace std;

struct Student {
    string name;
    int age;
};

class MyCompare {
public:
    bool operator()(const Student& s1, const Student& s2) const {  // Added const to method
        return s1.age > s2.age;
    }
};

void test01() {
    set<Student, MyCompare> s2;

    s2.insert({"张三", 10});
    s2.insert({"张五", 30});
    s2.insert({"张四", 20});
    s2.insert({"张七", 50});
    s2.insert({"张六", 40});

    for (auto it = s2.begin(); it != s2.end(); ++it) {  // Modern C++: Use auto and ++it
        cout << it->name << ": " << it->age << endl;
    }
    cout << endl;
}

int main() {
    test01();

    // system("pause");  // Consider removing or replacing for non-Windows compatibility
    cout << "Press Enter to continue..." << flush;  // Portable way to pause the program
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    return 0;
}

运行结果:


张七: 50
张六: 40
张五: 30
张四: 20
张三: 10

仿函数方法

using namespace std;
#include <iostream>

#include <set>

struct Student
{
	string name;
	int age;
};

class MyCompare
{
public:
	bool operator()(const Student &s1, const Student &s2)
	{
		return s1.age > s2.age;
	}
};

void test01()
{
	// 指定排序规则
	set<Student, MyCompare> s2;	// 仿函数方法

	s2.insert({"张三", 10});
	s2.insert({"张五", 30});
	s2.insert({"张四", 20});
	s2.insert({"张七", 50});
	s2.insert({"张六", 40});

	for (set<Student, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
	{
		cout << (*it).name << ": " << (*it).age << endl;;
	}
	cout << endl;
}

int main()
{

	test01();

	system("pause");

	return 0;
}

运行结果:

张七: 50
张六: 40
张五: 30
张四: 20
张三: 10

使用仿函数(Functor)或者普通函数指针,两种方法的比较(使用仿函数更加灵活,能方便实现更加复杂的比较)

使用普通函数指针和仿函数都可以实现自定义排序,但是它们的实现方式有所不同。

使用普通函数指针时,需要定义一个比较函数,该函数接受两个参数,返回一个bool类型的值,表示两个参数的大小关系。然后将该函数的指针作为参数传递给set的构造函数,set会根据该函数的比较结果进行排序。

示例代码如下:

bool cmp(int a, int b) {
    return a > b;
}

set<int, bool(*)(int, int)> s(cmp);

使用仿函数时,需要定义一个类,该类重载了()运算符,接受两个参数,返回一个bool类型的值,表示两个参数的大小关系。然后将该类的对象作为参数传递给set的构造函数,set会根据该对象的()运算符的比较结果进行排序。

示例代码如下:

struct cmp {
    bool operator()(int a, int b) const {
        return a > b;
    }
};

set<int, cmp> s;

使用仿函数的方式更加灵活,可以在类中定义一些成员变量和成员函数,实现更加复杂的排序方式。而使用函数指针的方式则比较简单,适用于比较简单的排序需求。

  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dontla

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值