STL-vector自定义排序(sort)和自定义去重(unique)

STL自定义排序(sort)和自定义去重(unique)

  • 自定义比较函数

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
class Link
{
public:
	int a;
	int b;
	string str;
};

int main()
{
	Link l1;
	l1.a = 3;
	l1.b = 4;
	l1.str = "hello";

	Link l2;
	l2.a = 3;
	l2.b = 2;
	l2.str = "bye";

	Link l3;
	l3.a = 4;
	l3.b = 5;
	l3.str = "good";

	Link l4;
	l4.a = 4;
	l4.b = 5;
	l4.str = "good";

	vector<Link> linkSet;
	linkSet.push_back(l1);
	linkSet.push_back(l2);
	linkSet.push_back(l3);
	linkSet.push_back(l4);

	cout << "排序前:"<<endl;
	//控制台输出
	for (int i = 0; i<linkSet.size(); i++)
	{
		cout << linkSet[i].a << " " << linkSet[i].b << " " << linkSet[i].str.c_str() << " " << endl;
	}

	//自定义删除
	auto cmp = [](Link x, Link y)
	{
			if (x.a == y.a)
				return x.b < y.b;
			return x.a < y.a;
	};
	sort (linkSet.begin(), linkSet.end(), cmp);

	cout << "排序后:" << endl;
	//控制台输出
	for (int i = 0; i<linkSet.size(); i++)
	{
		cout << linkSet[i].a << " " << linkSet[i].b << " " << linkSet[i].str.c_str() << " " << endl;
	}

	//自定义删除
	auto del= [](Link i, Link j)
	{	
		return i.a == j.a && i.b ==j.b;
	};
	std::vector<Link>::iterator last;
	last = unique(linkSet.begin(), linkSet.end(), del);
	linkSet.erase(last, linkSet.end());

	cout << "删除后:" << endl;
	//控制台输出
	for (int i = 0; i<linkSet.size(); i++)
	{
		cout << linkSet[i].a << " " << linkSet[i].b << " " << linkSet[i].str.c_str() << " " << endl;
	}
	system("pause");
	return 0;
}

运行结果如下图

 

  • 重载运算符,输出结果如上图

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
class Link
{
public:
	int a;
	int b;
	string str;
};

inline bool operator<(const Link& s1, const Link& s2)
{
	if (s1.a == s2.a)
		return s1.b < s2.b;
	return s1.a < s2.a;

}
inline bool operator==(const Link& s1, const Link& s2)
{
	return s1.a == s2.a && s1.b == s2.b;

}
int main()
{
	Link l1;
	l1.a = 3;
	l1.b = 4;
	l1.str = "hello";

	Link l2;
	l2.a = 3;
	l2.b = 2;
	l2.str = "bye";

	Link l3;
	l3.a = 4;
	l3.b = 5;
	l3.str = "good";

	Link l4;
	l4.a = 4;
	l4.b = 5;
	l4.str = "good";

	vector<Link> linkSet;
	linkSet.push_back(l1);
	linkSet.push_back(l2);
	linkSet.push_back(l3);
	linkSet.push_back(l4);

	cout << "排序前:" << endl;
	//控制台输出
	for (int i = 0; i < linkSet.size(); i++)
	{
		cout << linkSet[i].a << " " << linkSet[i].b << " " << linkSet[i].str.c_str() << " " << endl;
	}
	//自定义
	auto cmp = [](Link x, Link y)
	{
		if (x.a == y.a)
			return x.b < y.b;
		return x.a < y.a;
	};
	sort(linkSet.begin(), linkSet.end());

	cout << "排序后:" << endl;
	//控制台输出
	for (int i = 0; i < linkSet.size(); i++)
	{
		cout << linkSet[i].a << " " << linkSet[i].b << " " << linkSet[i].str.c_str() << " " << endl;
	}
	//自定义删除
	auto mycompare = [](Link i, Link j)
	{
		return i.a == j.a && i.b == j.b;
	};
	std::vector<Link>::iterator last;

	last = unique(linkSet.begin(), linkSet.end());
	linkSet.erase(last, linkSet.end());

	cout << "删除后:" << endl;
	//控制台输出
	for (int i = 0; i < linkSet.size(); i++)
	{
		cout << linkSet[i].a << " " << linkSet[i].b << " " << linkSet[i].str.c_str() << " " << endl;
	}
	system("pause");
	return 0;
}

 

 

可以使用STL库中的`std::unique`函数自定义类型的`vector`进行去重操作。但是在使用前需要定义一个比较函数(operator==),用于比较两个自定义类型是否相等。 以下是一个示例代码: ```c++ #include <iostream> #include <vector> #include <algorithm> using namespace std; struct Person { string name; int age; }; bool operator==(const Person& p1, const Person& p2) { return (p1.name == p2.name && p1.age == p2.age); } int main() { vector<Person> persons = {{"Alice", 18}, {"Bob", 20}, {"Alice", 18}, {"Cathy", 22}, {"Bob", 20}}; sort(persons.begin(), persons.end()); // 先排序,保证相同元素相邻 auto iter = unique(persons.begin(), persons.end()); persons.erase(iter, persons.end()); // 删除重复元素 for (const auto& person : persons) { cout << person.name << " " << person.age << endl; } return 0; } ``` 在这个例子中,我们定义了一个`Person`结构体,包含姓名和年龄两个成员变量。我们重载了`operator==`,在比较两个`Person`对象是否相等时,只有当它们的姓名和年龄都相等时才返回true。然后我们定义了一个`vector<Person>`类型的`persons`,其中包含了一些重复的`Person`对象。我们首先对`persons`进行了排序,这样相同的元素就相邻了。然后我们调用`unique`函数,它会返回一个指向第一个重复元素的迭代器,我们用这个迭代器来删除重复元素。最后我们遍历`persons`,输出结果。 注意:这个方法只能去除相邻的重复元素,如果有多个相同的元素分散在不同位置,就不能完全去重。如果需要完全去重,可以使用哈希表等数据结构实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值