STL标准库排序算法详解(上)

        本篇博客是本人休息过后,发现自己已经好几天没用动过手写东西了,哈哈哈所以最近可能会进行一段时间的频繁更新,这几天的博客都是干货满满哦!

嘿嘿,小侯不躺平也该醒醒去写一些自己了解的东西,顺便增强一下自己的知识储备了。

好了进入正文,该排序算法有很多内容所以采取了分开写代码来讲解,请仔细看代码段里的内容里面有对排序函数的详细解释。

代码如下:

#include<iostream>
#include<vector>
#include<list> 
#include<algorithm>
using namespace std;
/*
	这篇博客主讲STL中四大算法中的排序算法
	排序算法的特点是对容器的内容进行不同方式的排序
	例如sort() 
*/

void output(int val)
{
	cout << val << " ";
}
int main()
{
	vector<int> ivc1,ivc2;
	int n1,n2,x;
	cout << "请输入ivc1的容器大小:" << endl;
	cin >> n1;
	for(int i = 0; i < n1; i++)
	{
		cin >> x;
		ivc1.push_back(x);
	}
	cout << "请输入ivc2的容器大小:" << endl;
	cin >> n2;
	for(int i = 0; i < n2; i++)
	{
		cin >> x;
		ivc2.push_back(x);
	}
//	ivc2.resize(n);
//	copy(ivc1.begin(),ivc1.end(),ivc2.begin());
	cout << "ivc1 :" << endl;
	for_each(ivc1.begin(),ivc1.end(),output);
	cout << endl;
	cout << "ivc2 :" << endl;
	for_each(ivc2.begin(),ivc2.end(),output);
	cout << endl <<endl;
	
	/*
		binary_search(first,last,val) 二分法寻找 
		first,last是指向容器的迭代器
		val是要寻找的元素
		找到的话返回值为1,否则返回值为0 
	*/
	cout << "After binary_search():" << endl;
	if(binary_search(ivc1.begin(),ivc1.end(),5))
		cout << "Found(5)!" << endl;
	else 
		cout << "Not found(5)!" << endl;
	cout << endl;
	
	/*
		equal_range(first,last,val)
		返回两个迭代器即区间,val是要查询的值
		区间使用pair()可以包含两个多种类型的数据 
	*/ 
	cout << "After equal_range():" << endl;
	pair<vector<int>::iterator,vector<int>::iterator> it;
	it = equal_range(ivc1.begin(),ivc1.end(),7);
	cout << *it.first << " " << *it.second;
	cout << endl <<endl;
	
	/*
		includes(first,last,first2,last2)
		判断是否第二个容器是否被包含在第一个容器之中 
		被包含时返回值为1,否则返回值为0 
	*/ 
	cout << "After includes():" << endl;
	if(includes(ivc1.begin(),ivc1.end(),ivc2.begin(),ivc2.end()))
		cout << "Be include!" << endl;
	else
		cout << "Not be include!" << endl;
	cout << endl;
	
	/*
		lexicographical_compare(first,last,first2,last2)
		检查第一个范围 [first1, last1) 是否按字典序小于第二个范围 [first2, last2) 
			1.用 operator< 比较元素。
			2.用给定的二元比较函数 comp 比较函数。
		如果两个序列长度不同,并且
		短序列和长序列头部完全一样,
		例如example和examplee.
		那么,长度大的字典序比短序的大。
	*/
	cout << "After lexicographical_compare():" << endl;
	char name1[] = "limuge";
	char name2[] = "houyimei";	//按字典查找limuge比houyimei在后面 
	if(lexicographical_compare(name1,name1+6,name2,name2+8))
		cout << name1 <<" is less than " << name2 <<endl;
	else
		cout << name1 <<" is greater than " << name2 <<endl;	
	cout <<endl;
	if(lexicographical_compare(ivc1.begin(),ivc1.end(),ivc2.begin(),ivc2.end()))
		cout << "ivc1 is less than ivc2" <<endl;
	else
		cout << "ivc1 is greater than ivc2" <<endl;
	cout << endl;
	
	/*
		lower_bound(first,last,val)
		first和last分别指的是起始和终止的迭代器
		val指的是要查找的值,该函数的返回值是一个迭代器,返回大于等于val的第一个值的位置 
	*/
	cout << "After lower_bound(5):" << endl;
	int pos= lower_bound(ivc1.begin(), ivc1.end(), 5)-ivc1.begin();	//减去开始的位置迭代器,最终结果会是位置。 
	cout << pos << endl;
	cout << endl;
	
	/*
		upper_bound(first,last,val) 
		first和last分别指的是起始和终止的迭代器
		val指的是要查找的值,该函数的返回值是一个迭代器,返回大于等于val的最后的一个的位置 
		对应lower_bound()函数是upper_bound()函数,它返回大于等于key的最后一个元素
		也同样是要求容器,若数组中无重复元素,则两者返回值相同
	*/
	cout << "After upper_bound(5):" << endl;
	int ipos= upper_bound(ivc1.begin(), ivc1.end(), 5)-ivc1.begin();	//减去开始的位置迭代器,最终结果会是位置。 
	cout << ipos << endl;
	cout << endl;
	
	/*
		make_heap(first,last,参数)
		制造一个堆,可用伪函数less(),greater()来生成大顶堆和小顶堆 
		pop_heap(first,last)推出堆顶的元素,这里没有删除元素只是将堆顶元素和容器最后一个元素进行了替换 
		push_heap()将一个元素压进堆里,与make_heap用法差不多。 
 	*/
 	cout << "After make_heap(less()):" << endl;
 	make_heap(ivc2.begin(),ivc2.end(),less<int>());
 	for_each(ivc2.begin(),ivc2.end(),output);
 	cout << endl;
 	cout << "After make_heap(greater()):" << endl;
 	make_heap(ivc2.begin(),ivc2.end(),greater<int>());
 	for_each(ivc2.begin(),ivc2.end(),output);
 	cout << endl;
 	cout << "After push_heap(greater()):" << endl;
 	push_heap(ivc2.begin(),ivc2.end(),greater<int>());
 	for_each(ivc2.begin(),ivc2.end(),output);
 	cout << endl;
 	cout << "After pop_head():" << endl;
 	pop_heap(ivc2.begin(),ivc2.end());
 	for_each(ivc2.begin(),ivc2.end(),output);
 	cout << endl;
 	cout << "After sort_heap():" << endl;
 	sort_heap(ivc2.begin(),ivc2.end());
 	for_each(ivc2.begin(),ivc2.end(),output);
 	cout << endl;
 	cout << endl;
 	
 	/*
 		max_element(first,last)
		 返回first和last范围中最大值所在的位置 
		min_element(first,last)
		 返回first和last范围中最小值所在的位置  
	*/
	cout << "After max_element():" << endl;
	int max = max_element(ivc1.begin(),ivc1.end())-ivc1.begin();
	cout << max << endl;
	cout << "After min_element():" << endl;
	int min = min_element(ivc1.begin(),ivc1.end())-ivc1.begin();
	cout << min << endl;
	cout << endl;  	 
	
	/*
		merge(first,last,first2,last2,result)
		合并两个序列 ,result是存放合并后的序列容器开始的位置(即迭代器)
		注意要给存储数据的容器开辟空间,否则存不进去值 
	*/
	cout << "After merge():" << endl;
	vector<int> ivc3;
	ivc3.resize(ivc1.size()+ivc2.size()); 
	merge(ivc1.begin(),ivc1.end(),ivc2.begin(),ivc2.end(),ivc3.begin());
	for_each(ivc3.begin(),ivc3.end(),output);
	cout << endl; 
	cout << "内容就到这里结束了!,祝大家学业有成";
	return 0;
}

嘿嘿,请仔细看代码段里的内容哦,干货全部都在代码段里面。

运行后的结果如下:

 

 

好了,本人该去赶着去上思修课了哈哈很急,再见下一篇博客再见。

 大家努力攻克自己的技术难关加油哦,在各自的领域发光发热!!!

猜你喜欢

转载自blog.csdn.net/m0_61886762/article/details/124381744