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