[STL]vector与排序算法

vector与算法

头文件中包含大量与 vector 相关的算法,这些算法同样适用于其它容器,比如 std::list 等。

排序(Sort)

相关函数:
std::sort :普通排序

**default (1) ** * template void sort (RandomAccessIterator first, RandomAccessIterator last);*
custom (2) * template <class RandomAccessIterator, class Compare> void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);*

std::partial_sort:部分排序

**default (1) ** * template void partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);*
custom (2) * template <class RandomAccessIterator, class Compare> void partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);*

std::stable_sort:稳定排序

**default (1) ** template void stable_sort ( RandomAccessIterator first, RandomAccessIterator last );
custom (2) template <class RandomAccessIterator, class Compare> void stable_sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );

std::partial_sort_copy:部分排序,并拷贝

**default (1) ** *template <class InputIterator, class RandomAccessIterator> RandomAccessIterator partial_sort_copy (InputIterator first,InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last); *
custom (2) *template <class InputIterator, class RandomAccessIterator, class Compare> RandomAccessIterator partial_sort_copy (InputIterator first,InputIterator last, RandomAccessIterator result_first, RandomAccessIterator result_last, Compare comp); *

普通排序

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

bool lessArray(int a, int b)
{
	return a < b;
}

void initArray(vector<int> &ivec,int size)
{
	srand(unsigned(time(NULL)));
	for (int i = 0; i < size; ++i)
	{
		ivec.push_back(rand() % 10);
	}
}

void printArray(vector<int> ivec)
{
	vector<int>::iterator iter = ivec.begin();
	for (; iter != ivec.end(); ++iter)
	{
		cout << *iter << " ";
	}
	cout << endl;
}

int main()
{
	//普通方法
	vector<int> ivec1;
	initArray(ivec1, 5);
	cout << "ivec1 before sort:" << endl;
	printArray(ivec1);
	sort(ivec1.begin(), ivec1.end());  
	cout <<"ivec1 after sort" <<endl;
	printArray(ivec1);

	// 使用less<int>()
	vector<int> ivec2;
	initArray(ivec2, 10);
	cout << "ivec2 before sort:" << endl;
	printArray(ivec2);
	sort(ivec2.begin(), ivec2.end(),less<int>());
	cout << "ivec2 after sort" << endl;
	printArray(ivec2);
    
	//使用函数对象
	vector<int> ivec3;
	initArray(ivec3, 10);
	cout << "ivec3 before sort:" << endl;
	printArray(ivec3);
	sort(ivec3.begin(), ivec3.end(), lessArray);
	cout << "ivec3 after sort" << endl;
	printArray(ivec3);
	
	// lambda表达式
	vector<int> ivec4;
	initArray(ivec4, 10);
	cout << "ivec4 before sort:" << endl;
	printArray(ivec4);
	sort(ivec4.begin(), ivec4.end(), [](int a, int b){return a < b; });
	cout << "ivec4 after sort" << endl;
	printArray(ivec4);

	//仅对选定范围类的数据进行部分排序
	vector<int> ivec5;
	initArray(ivec5, 10);
	cout << "ivec5 before sort:" << endl;
	printArray(ivec5);
	partial_sort(ivec5.begin(), ivec5.begin()+7, ivec5.end());
	cout << "ivec5 after sort" << endl;
	printArray(ivec5);

	//稳定排序
	vector<int> ivec6(5,6);//初始化5个数值为6的数组
	srand(unsigned(time(NULL)));
	for (int i = 0; i < 6; ++i)
	{
		ivec6.push_back(rand()%10);
	}
	cout << "ivec6 before sort:" << endl;
	printArray(ivec6);
	stable_sort(ivec6.begin(), ivec6.end(), less<int>());
	cout << "ivec6 after sort" << endl;
	printArray(ivec6);

	//部分排序并拷贝
	int myints[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
	vector<int> ivec7(5);
	partial_sort_copy(myints, myints + 9, ivec7.begin(), ivec7.end());
	cout << "ivec7 after sort" << endl;
	printArray(ivec7);
    
	system("pause");
	return 0;
}


输出:

ivec1 before sort:
2 8 1 9 9
ivec1 after sort
1 2 8 9 9
ivec2 before sort:
2 8 1 9 9 1 8 1 3 4
ivec2 after sort
1 1 1 2 3 4 8 8 9 9
ivec3 before sort:
2 8 1 9 9 1 8 1 3 4
ivec3 after sort
1 1 1 2 3 4 8 8 9 9
ivec4 before sort:
2 8 1 9 9 1 8 1 3 4
ivec4 after sort
1 1 1 2 3 4 8 8 9 9
ivec5 before sort:
2 8 1 9 9 1 8 1 3 4
ivec5 after sort
1 1 1 2 3 4 8 9 9 8
ivec6 before sort:
6 6 6 6 6 2 8 1 9 9 1
ivec6 after sort
1 1 2 6 6 6 6 6 8 9 9
ivec7 after sort
1 2 3 4 5
请按任意键继续. . .

posted @ 2016-08-04 19:22  弦断  阅读(1484)  评论(0编辑  收藏  举报