Sort articles of C++STL common operations

Sort articles of C++STL common operations


Introduction:

#include<algorithm>

Sort sorting is the same faster sorting method as heap sorting, and its time complexity is O(n*logn).

Similar to quick sort

1. Simple understanding

vector:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> v1;
int a;
int main() {
    
    
	cout << "输入:";
	for (int i = 1; i <= 5; ++i) {
    
    
		cin >> a;
		v1.push_back(a);		//输入a并且压入到容器v1中
	}
	cout << "原始:";
	for (int i = 0; i < 5; ++i)
		cout << v1[i] << " ";	//输出原始的情况
	cout << endl;
	sort(v1.begin(), v1.end()); 
	cout << "排序后:";
	for (int i = 0; i < 5; ++i)
		cout << v1[i] << " ";	//输出使用sort排序好之后的情况
	cout << endl;
	return 0;
}

Insert picture description here

We see that after using sort, v1 is indeed sorted.

So what does the sort function use as a parameter?

The sort function has three parameters in total, the first is the starting position of the sorting sequence; the second is the position after the last position; the third is the sorting rule, and the third parameter is the ascending rule by default. Therefore, we used the begin and end of v1 as the first and second parameters above, and omitted the third parameter. After sorting, the internal v1 is in ascending order.

Ordinary array:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int x[10];
int main() {
    
    
	cout << "输入:";
	for (int i = 1; i <= 5; ++i)
		cin >> x[i];			//输入数据
	cout << "原始:";
	for (int i = 1; i <= 5; ++i)
		cout << x[i] << " ";	//输出原始的情况
	cout << endl;
	sort(x + 1, x + 6);			//注意这里是+6
	cout << "排序后:";
	for (int i = 1; i <= 5; ++i)
		cout << x[i] << " ";	//输出使用sort排序好之后的情况
	cout << endl;
	return 0;
}

Insert picture description here

Note that we are sorting the array x from subscript 1 to 5, but the second parameter of sort is an addition of 6.

If we add 5, the result is as follows:

Insert picture description here

We found that sort only sorts the first four numbers. This is what we mentioned earlier. The second parameter must be the position after the end of the sequence to be sorted (plus 1).

2. Sorting rules

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> v1;
int a;
int main() {
    
    
	cout << "输入:";
	for (int i = 1; i <= 5; ++i) {
    
    
		cin >> a;
		v1.push_back(a);		//输入a并且压入到容器v1中
	}
	cout << "原始:";
	for (int i = 0; i < 5; ++i)
		cout << v1[i] << " ";	//输出原始的情况
	cout << endl;
	sort(v1.begin(), v1.end(),less<int>());			//less从小到大
	cout << "从小到大:";
	for (int i = 0; i < 5; ++i)
		cout << v1[i] << " ";	//输出使用sort排序好之后的情况
	cout << endl;
	sort(v1.begin(), v1.end(), greater<int>());		//greater从大到小
	cout << "从大到小:";
	for (int i = 0; i < 5; ++i)
		cout << v1[i] << " ";	//输出使用sort排序好之后的情况
	cout << endl;
	return 0;
}

Insert picture description here

Less is from small to large, and greater is from large to small. Because the default is from small to large, the third parameter is generally omitted.

3. Custom rules

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct Node {
    
    
	int data1;
	int data2;
};
vector<Node> v;
Node a;
bool cmp(Node a, Node b) {
    
    
	if (a.data1 == b.data1)
		return a.data2 > b.data2;
	return a.data1 > b.data1;
}
//cmp将排序规则定为按照data1降序,若data1相等按data2降序
int main() {
    
    
	cout << "输入:" << endl;
	for (int i = 1; i <= 4; ++i) {
    
    
		cin >> a.data1 >> a.data2;
		v.push_back(a);				//输入a并且压入到容器v1中
	}
	sort(v.begin(), v.end(), cmp);	//按照cmp规则进行排序
	cout << "结果:" << endl;
	for (int i = 0; i < 4; ++i)
		cout << v[i].data1 << " " << v[i].data2 << endl;	//输出排好序的v1
	return 0;
}

Insert picture description here

Here we use the cmp function to set the sorting rule as follows: descending order according to data1, if data1 is equal, descending order according to data2.

And use cmp as the third parameter of sort.


If you find any problems, please correct me!

Please leave a message if you don’t understand!

Guess you like

Origin blog.csdn.net/qq_45985728/article/details/113048880