1. 程式人生 > >STL演算法---排序演算法(二)

STL演算法---排序演算法(二)

4. random_shuffle(隨機調整次序)

對指定範圍內的元素隨機調整次序。
過載版本輸入一個隨機數產生操作
函式原形
template<class RanIt> void random_shuffle(RanIt first, RanIt last);
template<class RanIt, class Fun> void random_shuffle(RanIt first, RanIt last, Fun& f);
/////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
	std::vector<int> nV2, nV1;
	std::vector<int>::iterator iter;

	nV1.clear();
	nV1.push_back(14);
	nV1.push_back(16);
	nV1.push_back(15);
	nV1.push_back(11);
	nV1.push_back(10);
	nV1.push_back(13);
	nV1.push_back(-2);
	nV1.push_back(27);
	nV1.push_back(24);
	nV1.push_back(-1);
	nV1.push_back(19);
	nV1.push_back(19);

	// 排序前: 14, 16, 15, 11, 10, 13, -2, 27, 24, -1, 19, 19
	std::random_shuffle(nV1.begin(), nV1.end());
	// 排序後: 19, 16, -1, 15, 14, 19, 27, 11, 10, -2, 24, 13

	nV1.clear();
	nV1.push_back(14);
	nV1.push_back(16);
	nV1.push_back(15);
	nV1.push_back(11);
	nV1.push_back(10);
	nV1.push_back(13);
	nV1.push_back(-2);
	nV1.push_back(27);
	nV1.push_back(24);
	nV1.push_back(-1);
	nV1.push_back(19);
	nV1.push_back(19);

	// 排序前: 14, 16, 15, 11, 10, 13, -2, 27, 24, -1, 19, 19
	std::random_shuffle(nV1.begin(), nV1.end());
	// 排序後: 19, 10, 16, 24, 15, 13, -1, 19, 11, 27, -2, 14
	
	return 0;
}

5. reverse / reverse_copy(反序排序)

5.1 reverse

將指定範圍內元素重新反序排序
函式原形
template<class BidIt> void reverse(BidIt first, BidIt last);


5.2 reverse_copy

與reverse類似,不過將結果寫入另一個容器
函式原形
template<class BidIt, class OutIt> OutIt reverse_copy(BidIt first, BidIt last, OutIt x);

/////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
	std::vector<int> nV2, nV1;
	std::vector<int>::iterator iter;

	nV1.clear();
	nV1.push_back(14);
	nV1.push_back(16);
	nV1.push_back(15);
	nV1.push_back(11);
	nV1.push_back(10);
	nV1.push_back(13);
	nV1.push_back(-2);
	nV1.push_back(27);
	nV1.push_back(24);
	nV1.push_back(-1);
	nV1.push_back(19);
	nV1.push_back(19);

	// 排序前: 14, 16, 15, 11, 10, 13, -2, 27, 24, -1, 19, 19
	std::reverse(nV1.begin(), nV1.end());
	// 排序後: 19, 19, -1, 24, 27, -2, 13, 10, 11, 15, 16, 14

	nV1.clear();
	nV1.push_back(14);
	nV1.push_back(16);
	nV1.push_back(15);
	nV1.push_back(11);
	nV1.push_back(10);
	nV1.push_back(13);
	nV1.push_back(-2);
	nV1.push_back(27);
	nV1.push_back(24);
	nV1.push_back(-1);
	nV1.push_back(19);
	nV1.push_back(19);

	// 排序前nV1: 14, 16, 15, 11, 10, 13, -2, 27, 24, -1, 19, 19
	nV2.resize(nV1.size());
	iter = std::reverse_copy(nV1.begin(), nV1.end(), nV2.begin());
	// 排序前nV1: 14, 16, 15, 11, 10, 13, -2, 27, 24, -1, 19, 19
	// 排序後nV2: 19, 19, -1, 24, 27, -2, 13, 10, 11, 15, 16, 14, end(iter)

	return 0;
}

6. sort/ stable_sort

6.1 sort

以升序重新排列指定範圍內的元素。
過載版本使用自定義的比較操作
函式原形
template<class RanIt> void sort(RanIt first, RanIt last);
template<class RanIt, class Pred> void sort(RanIt first, RanIt last, Pred pr);


6.2 stable_sort

與sort類似,但保留相等元素之間的順序關係
函式原形
template<class BidIt> void stable_sort(BidIt first, BidIt last);
template<class BidIt, class Pred> void stable_sort(BidIt first, BidIt last, Pred pr);


/////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
	std::vector<int> nV2, nV1;
	std::vector<int>::iterator iter;

	nV1.clear();
	nV1.push_back(14);
	nV1.push_back(16);
	nV1.push_back(15);
	nV1.push_back(11);
	nV1.push_back(10);
	nV1.push_back(13);
	nV1.push_back(-2);
	nV1.push_back(27);
	nV1.push_back(24);
	nV1.push_back(-1);
	nV1.push_back(19);
	nV1.push_back(19);

	// 排序前: 14, 16, 15, 11, 10, 13, -2, 27, 24, -1, 19, 19
	std::sort(nV1.begin(), nV1.end());
	// 排序後: -2, -1, 10, 11, 13, 14, 15, 16, 19, 19, 24, 27

	nV1.clear();
	nV1.push_back(14);
	nV1.push_back(16);
	nV1.push_back(15);
	nV1.push_back(11);
	nV1.push_back(10);
	nV1.push_back(13);
	nV1.push_back(-2);
	nV1.push_back(27);
	nV1.push_back(24);
	nV1.push_back(-1);
	nV1.push_back(19);
	nV1.push_back(19);

	// 排序前: 14, 16, 15, 11, 10, 13, -2, 27, 24, -1, 19, 19
	std::stable_sort(nV1.begin(), nV1.end());
	// 排序後: -2, -1, 10, 11, 13, 14, 15, 16, 19, 19, 24, 27
	return 0;
}

7. rotate / rotate_copy

7.1 rotate

把[first, middle)的元素拷貝到last的後面; 或者說把[middle, last)的元素拷貝到first的前面.
template<class FwdIt> void rotate(FwdIt first, FwdIt middle, FwdIt last);

7.2 rotate_copy

與rotate類似,不過將結果寫入另一個容器
函式原形
template<class FwdIt, class OutIt> OutIt rotate_copy(FwdIt first, FwdIt middle, FwdIt last, OutIt x);
/////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
	std::vector<int> nV2, nV1;
	std::vector<int>::iterator iter;

	nV1.clear();
	nV1.push_back(14);
	nV1.push_back(16);
	nV1.push_back(15);
	nV1.push_back(11);
	nV1.push_back(10);
	nV1.push_back(13);
	nV1.push_back(-2);
	nV1.push_back(27);
	nV1.push_back(24);
	nV1.push_back(-1);
	nV1.push_back(19);
	nV1.push_back(19);

	// 排序前: 14, 16, 15, 11, 10(middle), 13, -2, 27, 24, -1, 19, 19
	std::rotate(nV1.begin(), nV1.begin() + 4, nV1.end());
	// 排序後: 10, 13, -2, 27, 24, -1, 19, 19, 14, 16, 15, 11

	nV1.clear();
	nV1.push_back(14);
	nV1.push_back(16);
	nV1.push_back(15);
	nV1.push_back(11);
	nV1.push_back(10);
	nV1.push_back(13);
	nV1.push_back(-2);
	nV1.push_back(27);
	nV1.push_back(24);
	nV1.push_back(-1);
	nV1.push_back(19);
	nV1.push_back(19);
	nV2.resize(nV1.size());
	// 排序前: 14, 16, 15, 11, 10(middle), 13, -2, 27, 24, -1, 19, 19
	std::rotate_copy(nV1.begin(), nV1.begin() + 4, nV1.end(), nV2.begin());
	// 排序後nV2: 10, 13, -2, 27, 24, -1, 19, 19, 14, 16, 15, 11, end(iter)

	return 0;
}

8. inplace_merge / merge

8.1 inplace_merge

合併兩個有序序列,結果序列覆蓋兩端範圍。
過載版本使用輸入的操作進行排序
函式原形
template<class BidIt> void inplace_merge(BidIt first, BidIt middle, BidIt last);
template<class BidIt, class Pred> void inplace_merge(BidIt first, BidIt middle, BidIt last, Pred pr);

8.2 merge

合併兩個有序序列,存放到另一個序列。過載版本使用自定義的比較
函式原形
template<class InIt1, class InIt2, class OutIt> OutIt merge(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2, OutIt x);
template<class InIt1, class InIt2, class OutIt, class Pred> OutIt merge(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2, OutIt x, Pred pr);

/////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
	std::vector<int> nV2, nV1, nV3;
	std::vector<int>::iterator iter;

	nV1.clear();
	nV1.push_back(12);
	nV1.push_back(13);
	nV1.push_back(14);
	nV1.push_back(15);
	nV1.push_back(50);
	nV1.push_back(7);		// --
	nV1.push_back(23);
	nV1.push_back(27);
	nV1.push_back(28);
	nV1.push_back(30);
	nV1.push_back(34);
	nV1.push_back(35);
	nV1.push_back(37);

	// nV1中包含了兩組有序序列
	// 排序前: 12, 13, 14, 15, 50, 7(middle), 23, 27, 28, 30, 34, 35, 37
	std::inplace_merge(nV1.begin(), nV1.begin() + 5, nV1.end());
	// 排序後: 7, 12, 13, 14, 15, 23, 27, 28, 30, 34, 35, 37, 50

	// 升序
	nV1.clear();
	nV1.push_back(14);
	nV1.push_back(16);
	nV1.push_back(18);
	nV1.push_back(20);

	// 升序
	nV2.clear();
	nV2.push_back(-2);
	nV2.push_back(13);
	nV2.push_back(20);
	nV2.push_back(25);
	nV2.push_back(26);
	nV3.resize(nV1.size() + nV2.size());
	iter = std::merge(nV1.begin(), nV1.end(), nV2.begin(), nV2.end(), nV3.begin());
	// 排序後nV3: -2, 13, 14, 16, 18, 20, 20, 25, 26, end(iter)
	return 0;
}