STL常用排序算法

170 阅读1分钟

常用排序算法

算法简介:

  • sort //对容器内元素进行排序
  • random_shuffle //洗牌 指定范围内的元素随机调整次数
  • merge //容器元素合并, 并存储到另一容器中
  • reverse //反转指定范文的元素

函数原型:

  • sort(iterator beg , iterator end , _Pred);
  • //按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
  • //beg开始迭代器
  • //end结束迭代器
  • //_Pred谓词
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>

void MyPrint(int val)
{
	cout << val << "  ";
}

void test01()
{
	vector<int>v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(50);
	v.push_back(40);
	v.push_back(30);

	//利用sort进行升序
	sort(v.begin(), v.end());
	for_each(v.begin(), v.end(), MyPrint);
	cout << endl;


	//改变为  降序
	sort(v.begin(), v.end(), greater<int>());
	for_each(v.begin(), v.end(), MyPrint);
	cout << endl;
}


int main()
{

	test01();

	system("pause");
	return 0;
}

函数原型:

  • random_shuddle(iterator beg , iterator end);
  • //指定范围内的元素随机调整次序
  • //beg开始迭代器
  • //end结束迭代器
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<ctime>

void MyPrint(int val)
{
	cout << val <<" ";
}

void test01()
{
	srand((unsigned int)time(NULL));
	vector<int>v;
	for (int i = 0;i < 10;i++)
	{
		v.push_back(i);
	}

	//利用洗牌	算法 打乱排序
	random_shuffle(v.begin(), v.end());

	for_each(v.begin(), v.end(), MyPrint);
	cout << endl;
}

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

函数原型:

  • merge(iterator beg1 , iterator end1 , iterator beg2, iterator end2 , iterator dest);
  • //容器元素合并,并存储到另一容器中
  • //注意两个容器必须是有序的
  • //beg1始迭代器
  • //end1束迭代器
  • //beg2始迭代器
  • //end2束迭代器
  • //dest目标容器开始迭代器

示例:

#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>

void MyPrint(const int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int>v1;
	vector<int>v2;

	for (int i = 0;i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i + 1);
	}

	//目标容器
	vector<int>vTarget;
	//提前给目标容器分配空间
	vTarget.resize(v1.size() + v2.size());

	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());

	for_each(vTarget.begin(), vTarget.end(), MyPrint);
	cout << endl;
	
}

int main()
{
	test01();

	system("pause");
	return 0;
}

函数原型:

  • reverse(iterator beg , iterator end);
  • //反转指定范围的元素
  • //beg开始迭代器
  • //end结束迭代器
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>

void MyPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int>v;
	v.push_back(10);
	v.push_back(30);
	v.push_back(50);
	v.push_back(20);
	v.push_back(40);

	cout << "反转前:" << endl;
	for_each(v.begin(), v.end(), MyPrint);
	cout << endl;

	cout << "反转后:" << endl;
	reverse(v.begin(), v.end());
	for_each(v.begin(), v.end(), MyPrint);
	cout << endl;
}

void test02()
{
	vector<int>v1;
	vector<int>v2;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v2.push_back(100);
	v2.push_back(200);
	v2.push_back(300);

	vector<int>vTarget;
	//给vTarget开辟新空间
	vTarget.resize(v1.size() + v2.size());
	//将v1和v2合并到新容器vTarget中
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
	cout << "反转前:" << endl;
	for_each(vTarget.begin(), vTarget.end(), MyPrint);
	cout << endl;

	cout << "反转后:" << endl;
	reverse(vTarget.begin(), vTarget.end());
	for_each(vTarget.begin(), vTarget.end(), MyPrint);
	cout << endl;
}
int main()
{
	//test01();
	test02();

	system("pause");
	return 0;
}