#include <iostream>
#include <algorithm>
#include <functional> //for greater
#include <iterator>
#include <vector>
#include <math.h>
#include <assert.h>
class Job
{
public:
enum Priority_code{ standard, normal, high };
Job(int i, Priority_code pc = standard)
: id(i), pri(pc){ }
Priority_code getPri() const { return pri; }
int getId() const { return id; }
friend std::ostream& operator<<(std::ostream& os, const Job& job)
{
os << job.getId() << "(" << job.getPri() << ")";
return os;
}
friend bool operator<(const Job& job1, const Job& job2)
{
return job1.getPri() > job2.getPri();
}
public:
Priority_code pri;
int id;
};
//排序算法
int main()
{
/************************************************************************/
//sort
/************************************************************************/
/*
sort:以非递减的顺序进行排序,
sort是个不稳定的排序算法,假设[first,last)中存在两个相等的元素,sort排完序之后不能保证这两个元素的相对位置还和原来一样
版本1:使用operator<来比较,必导致is_sorted(first,last)为true
版本2:采用function object comp来比较,必导致is_sorted(first,last,comp)为true
*/
/*
template<class RandomAccessIterator>
void sort(
RandomAccessIterator _First,
RandomAccessIterator _Last
);
template<class RandomAccessIterator, class Pr>
void sort(
RandomAccessIterator _First,
RandomAccessIterator _Last,
BinaryPredicate _Comp
);
*/
int A[] = { 1, 4, 8, 2, 6, 5, 7 };
std::sort(std::begin(A), std::end(A));
assert(std::is_sorted(std::begin(A), std::end(A)));
//1 2 4 5 6 7 8
std::for_each(std::begin(A), std::end(A), [](const int i){std::cout << i << " "; });
std::cout << std::endl;
int B[] = { 1, 4, 8, 2, 6, 5, 7 };
std::sort(std::begin(B), std::end(B), std::greater<int>());
assert(std::is_sorted(std::begin(B), std::end(B), std::greater<int>()));
//8 7 6 5 4 2 1
std::for_each(std::begin(B), std::end(B), [](const int i){std::cout << i << " "; });
std::cout << std::endl;
int C[] = { 1, -4, 8, -2, -6, 5, 7 };
std::sort(std::begin(C), std::end(C),[](const int v1, const int v2)
{
return abs(v1) > abs(v2);
});
//8 7 -6 5 -4 -2 1
std::for_each(std::begin(C), std::end(C), [](const int i){std::cout << i << " "; });
std::cout << std::endl;
/************************************************************************/
//stable_sort
/************************************************************************/
//stable_sort:与sort类似,但是stable_sort是个稳定的排序算法,即能保证两个相等的元素在排完序之后相对位置保持不变
/*
template<class BidirectionalIterator>
void stable_sort(
BidirectionalIterator _First,
BidirectionalIterator _Last
);
template<class BidirectionalIterator, class BinaryPredicate>
void stable_sort(
BidirectionalIterator _First,
BidirectionalIterator _Last,
BinaryPredicate _Comp
);
*/
char s[] = "fdBeACFDbEac";
std::stable_sort(s, s + 12,[](const char c1, const char c2)
{
return tolower(c1) < tolower(c2);
});
std::cout << s << std::endl; //AaBbCcdDeEfF
std::cout << "==============================" << std::endl;
std::vector<Job> jobs;
jobs.push_back(Job(1, Job::normal));
jobs.push_back(Job(2, Job::standard));
jobs.push_back(Job(3, Job::normal));
jobs.push_back(Job(4, Job::high));
jobs.push_back(Job(5));
jobs.push_back(Job(6, Job::high));
std::stable_sort(jobs.begin(), jobs.end());
std::copy(jobs.begin(), jobs.end(), std::ostream_iterator<Job>(std::cout, "\n"));
std::cout << "==============================" << std::endl;
/************************************************************************/
//partial_sort
/************************************************************************/
/*
partial_sort:重新排列[first,last),使得[first,sortEnd)按照递增顺序排列,其余的可以不按照特定顺序排列,如果_SortEnd==_First,即不做任何的排序;如果_SortEnd==_Last,即把整个[first,last)进行排序
*/
/*
template<class RandomAccessIterator>
void partial_sort(
RandomAccessIterator _First,
RandomAccessIterator _SortEnd,
RandomAccessIterator _Last
);
template<class RandomAccessIterator, class BinaryPredicate>
void partial_sort(
RandomAccessIterator _First,
RandomAccessIterator _SortEnd,
RandomAccessIterator _Last
BinaryPredicate _Comp
);
*/
//试找寻数组中前5个最小值,及A[0]~A[4]以非递减的顺序排序,其余不保证顺序
int AA[] = { 7, 2, 6, 11, 9, 3, 12, 10, 8, 4, 1, 5 };
int *p = std::begin(AA);
std::advance(p, 5);
std::partial_sort(std::begin(AA), p, std::end(AA));
//1 2 3 4 5 11 12 10 9 8 7 6
std::copy(std::begin(AA), std::end(AA), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
/************************************************************************/
//partial_sort_copy
/************************************************************************/
/*
partial_sort_copy:函数会从[first1,last1)内复制N个最小元素到[first2,last2)之中,
这里的N是last1-first1和last2-first2两者中的最小值,被复制过去的元素以递增顺序排列
*/
/*
template<class InputIterator, class RandomAccessIterator>
RandomAccessIterator partial_sort_copy(
InputIterator _First1,
InputIterator _Last1,
RandomAccessIterator _First2,
RandomAccessIterator _Last2
);
template<class InputIterator, class RandomAccessIterator, class BinaryPredicate>
RandomAccessIterator partial_sort_copy(
InputIterator _First1,
InputIterator _Last1,
RandomAccessIterator _First2,
RandomAccessIterator _Last2,
BinaryPredicate _Comp
);
*/
//将数组BB中的前4个最小值复制到一个vector中去
int BB[] = { 7, 2, 6, 11, 9, 3, 12, 10, 8, 4, 1, 5 };
std::vector<int> iv(4);
std::partial_sort_copy(std::begin(BB), std::end(BB),iv.begin(), iv.end());
//1 2 3 4
std::copy(iv.begin(), iv.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
/************************************************************************/
//nth_element
/************************************************************************/
/*
nth_element:函数重新排列[first,last),使得iterator _Nth所指元素与整个[first,last)排序后的同一位置的元素相同,此外,也保证了[nth,last)内的任意元素都大于[first,nth)内的任意元素,而并不保证[first,nth)与[nth,last)内的元素保持一种顺序
*/
/*
template<class RandomAccessIterator>
void nth_element(
RandomAccessIterator _First,
RandomAccessIterator _Nth,
RandomAccessIterator _Last
);
template<class RandomAccessIterator, class BinaryPredicate>
void nth_element(
RandomAccessIterator _First,
RandomAccessIterator _Nth,
RandomAccessIterator _Last,
BinaryPredicate _Comp
);
*/
int CC[] = { 7, 2, 6, 11, 9, 3, 12, 10, 8, 4, 1, 5 };
int *pp = std::begin(CC);
std::advance(pp, 4);
std::nth_element(std::begin(CC), pp, std::end(CC));
//?????
std::copy(std::begin(CC), std::end(CC), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
/************************************************************************/
//is_sorted
/************************************************************************/
//is_sorted:用来测试某个序列是否已经排好序,已排好返回true,反之返回false
/*
template <class ForwardIterator>
bool is_sorted(
ForwardIterator _First,
ForwardIterator _Last
);
template <class ForwardIterator, class StrictWeakOrdering>
bool is_sorted(
ForwardIterator _First,
ForwardIterator _Last,
StrictWeakOrdering _Comp
);
*/
//略
return 0;
}
运算结果:
1 2 4 5 6 7 8
8 7 6 5 4 2 1
8 7 -6 5 -4 -2 1
AaBbCcdDeEfF
===========================
4(2)
6(2)
1(1)
3(1)
2(0)
5(0)
===========================
1 2 3 4 5 11 12 10 9 8 7 6
1 2 3 4
1 2 3 4 5 6 7 8 9 10 11 12
====================打个广告,欢迎关注====================
QQ: | 412425870 |
csdn博客: | http://blog.csdn.net/caychen |
码云: | https://gitee.com/caychen/ |
github: | https://github.com/caychen |
点击群号或者扫描二维码即可加入QQ群: | |