1.vector容器(仿函数排序)

class MyCompare
{
public:
	bool operator()(int num1, int num2)
	{
		return num1 > num2;
	}
};
void test01()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(40);
	v.push_back(20);
	v.push_back(30);
	v.push_back(50);
	//默认从小到大
	sort(v.begin(), v.end());
	//使用函数对象改变算法策略,排序从大到小
	sort(v.begin(), v.end(), MyCompare());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
int main() {
	test01();
	system("pause");
	return 0;
}

小结:deque容器和vector容器排序用法一样,因为都支持随机访问。例如:sort(v.begin(), v.end());

2.list容器

void printList(const list<int>& L) {

	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

bool myCompare(int val1 , int val2)
{
	return val1 > val2;
}

//反转和排序
void test01()
{
	list<int> L;
	L.push_back(90);
	L.push_back(30);
	L.push_back(20);
	L.push_back(70);
	printList(L);

	//反转容器的元素
	L.reverse();
	printList(L);

	//排序
	L.sort(); //默认的排序规则 从小到大
	printList(L);

	L.sort(myCompare); //指定规则,从大到小
	printList(L);
}

int main() {
	test01();
	system("pause");
	return 0;
}

 小结:list不支持随机访问,用sort前,需要指定具体容器。例如L.sort()

3.set存放内置数据类型排序

class MyCompare 
{
public:
	bool operator()(int v1, int v2) {
		return v1 > v2;
	}
};
void test01() 
{    
	set<int> s1;
	s1.insert(10);
	s1.insert(40);
	s1.insert(20);
	s1.insert(30);
	s1.insert(50);

	//默认从小到大
	for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;

	//指定排序规则
	set<int,MyCompare> s2;
	s2.insert(10);
	s2.insert(40);
	s2.insert(20);
	s2.insert(30);
	s2.insert(50);

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

int main() {
	test01();
	system("pause");
	return 0;
}

小结:set容器本身就默认从小到大排序,更改排序规则需要利用仿函数MyCompare。

特别注意三点:

(1)声明定义仿函数class MyCompare 

(2)定义容器set<int,MyCompare> s2;

(3)遍历容器:for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++) 

4.set容器自定义类按指定变量排序

class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	string m_Name;
	int m_Age;

};
class comparePerson
{
public:
	bool operator()(const Person& p1, const Person &p2)
	{
		//按照年龄进行排序  降序
		return p1.m_Age > p2.m_Age;
	}
};

void test01()
{
	set<Person, comparePerson> s;

	Person p1("刘备", 23);
	Person p2("关羽", 27);
	Person p3("张飞", 25);
	Person p4("赵云", 21);

	s.insert(p1);
	s.insert(p2);
	s.insert(p3);
	s.insert(p4);

	for (set<Person, comparePerson>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << "姓名: " << it->m_Name << " 年龄: " << it->m_Age << endl;
	}
}
int main() {
	test01();
	system("pause");
	return 0;
}

5.map存放内置数据类型排序

class MyCompare {
public:
	bool operator()(int v1, int v2) {
		return v1 > v2;
	}
};

void test01() 
{
	//默认从小到大排序
	//利用仿函数实现从大到小排序
	map<int, int, MyCompare> m;

	m.insert(make_pair(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(3, 30));
	m.insert(make_pair(4, 40));
	m.insert(make_pair(5, 50));

	for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++) {
		cout << "key:" << it->first << " value:" << it->second << endl;
	}
}
int main() {
	test01();
	system("pause");
	return 0;
}

6.map容器自定义类按指定变量排序

#include<iostream>
using namespace std;
#include <map>
#include <string>
class Person
{
public:
	Person(string name, int age, int height)
	{
		this->m_Name = name;
		this->m_Age = age;
		this->m_Height = height;
	}
	string m_Name;
	int m_Age;
	int m_Height;
};
class Wife{
public:
	Wife(string wname, int wage)
	{
		this->w_Name = wname;
		this->w_Age = wage;

	}
	string w_Name;
	int w_Age;
};
class comparePerson
{
public:
	bool operator()(const Person& p1, const Person& p2)
	{
		if (p1.m_Age == p2.m_Age)
		{
			return p1.m_Height > p2.m_Height;
		}
		return p1.m_Age < p2.m_Age;
	}

};
void myprint(map<Person, Wife, comparePerson>&m)
{
	for (map<Person, Wife, comparePerson>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "\t姓名:" << it->first.m_Name << "\t年龄:" << it->first.m_Age
			<< "\t身高:" << it->first.m_Height << "\t妻子:" << it->second.w_Name
			<< "\t年龄:" << it->second.w_Age
			<< endl;
	}
}
void test01() {

	map<Person,Wife,comparePerson>m;
	Person p1("刘备", 35, 175);
	Person p2("曹操", 45, 180);
	Person p3("孙权", 40, 170);
	Person p4("赵云", 25, 190);
	Person p5("张飞", 35, 160);
	Person p6("关羽", 35, 200);
	Wife w1("张三",23);
	Wife w2("李四",24);
	Wife w3("王五",25);
	Wife w4("马六",26);
	Wife w5("赵七",27);
	Wife w6("老八",28);

	m.insert(pair<Person,Wife>(p1, w1));
	m.insert(pair<Person,Wife>(p2, w2));
	m.insert(pair<Person,Wife>(p3, w3));
	m.insert(pair<Person,Wife>(p4, w4));
	m.insert(pair<Person,Wife>(p5, w5));
	m.insert(pair<Person,Wife>(p6, w6));
	myprint(m);
}

int main() {
	test01();
	system("pause");
	return 0;
}

小结:map容器排序和 set相似。

特别注意三点:

(1)容器的声明和遍历时,多了一个参数,也就是3个参数,因为他是以对组的形式存在。

(2)遍历容器输出内容时,注意想要输入的第一个Person还是第二个Wife。

cout << "\t姓名:" << it->first.m_Name << "\t年龄:" << it->first.m_Age
            << "\t身高:" << it->first.m_Height << "\t妻子:" << it->second.w_Name
            << "\t年龄:" << it->second.w_Age
            << endl;

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐