partial_sort接受一个middle迭代器,使序列中的middle-first个最小元素以递增顺序排序,置于[first, middle)内。
template <class RandomAccessIterator>
void partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last);
//包含比较函数的声明
template <class RandomAccessIterator, class Compare>
void partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
示例代码:
int a[] = {1,4,3,6,7,2,5};
vector<int> v1(a, a + 7);
partial_sort(v1.begin(), v1.begin() + 3, v1.end());
参数意思和partial_sort()函数一样,多出来的是将排序的这部分存入到以result_first为首指针的数组中去(不超过result_last 尾指针),如果原来存储了内容,就覆盖。
template <class InputIterator, class RandomAccessIterator>
RandomAccessIterator
partial_sort_copy ( InputIterator first,InputIterator last,
RandomAccessIterator result_first,
RandomAccessIterator result_last );
//返回值:指向结果序列中写入的最后一个元素之后的元素的迭代器。
//包含比较函数的声明
template <class InputIterator, class RandomAccessIterator, class Compare>
RandomAccessIterator
partial_sort_copy ( InputIterator first,InputIterator last,
RandomAccessIterator result_first,
RandomAccessIterator result_last,
Compare comp );
//返回值:指向结果序列中写入的最后一个元素之后的元素的迭代器。
示例代码:
int a[] = {1,4,3,6,7,2,5};
vector<int> v1(a, a + 7), v2;
vector<int>::iterator i1;
i1 = partial_sort_copy(v1.begin(), v1.begin() + 4, v2.begin(), v2.end());
对给定区间所有元素进行排序
template <class RandomAccessIterator>
void sort (RandomAccessIterator first, RandomAccessIterator last);
//包含比较函数的声明
template <class RandomAccessIterator, class Compare>
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
示例代码:
int a[] = {1,4,3,6,7,2,5};
vector<int> v1(a, a + 7);
sort(v1.begin(), v1.begin() + 3, v1.end());
stable_sort()可以对vector的成员进行排序,可保证相等元素的原本相对次序在排序后保持不变
template <class RandomAccessIterator>
void stable_sort ( RandomAccessIterator first, RandomAccessIterator last );
//包含比较函数的声明
template <class RandomAccessIterator, class Compare>
void stable_sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );
示例代码:
int a[] = {1,4,3,6,7,2,5};
vector<int> v1(a, a + 7);
stable_sort(v1.begin(), v1.begin() + 3, v1.end());
将一个堆做排序,最终成为一个有序的系列,可以看到sort_heap时,必须先是一个堆(两个特性:1、最大元素在第一个 2、添加或者删除元素以对数时间),因此必须先做一次make_heap.
几个相关的函数:
template <class RandomAccessIterator>
void sort_heap (RandomAccessIterator first, RandomAccessIterator last);
示例代码:
int a[] = {1,4,3,6,7,2,5};
vector<int> v1(a, a + 7);
make_heap(v1.begin(), v1.end());
sort_heap(v1.begin(), v1.end());
//包含比较函数的声明
template <class RandomAccessIterator, class Compare>
void sort_heap (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
20180423新增
对于heap算法来说,其只能进行元素的排列,不能进行元素的增删,例如
template <typename T>
struct display{
void operator()(T &x){
cout << x << " ";
}
};
int main()
{
int arr[]={0,1,2,3,4,8,9,3,5};
vector<int> vec(arr, arr+sizeof(arr)/4);
make_heap(vec.begin(), vec.end());
cout << "make_heap:";
for_each(vec.begin(), vec.end(), display<int>());
cout << endl;
vec.push_back(25);
push_heap(vec.begin(), vec.end());
cout << "push_heap:";
for_each(vec.begin(), vec.end(), display<int>());
cout << endl;
pop_heap(vec.begin(), vec.end());
cout << "pop_heap:";
for_each(vec.begin(), vec.end(), display<int>());
cout << endl;
vec.pop_back();
sort_heap(vec.begin(), vec.end());
cout << "sort_heap:";
for_each(vec.begin(), vec.end(), display<int>());
cout << endl;
return 0;
}
在进行make_heap()
后,vector
内的元素已经按照堆(二叉搜索树)的顺序排好,如果这时候要增加元素,需要对vector
进行push_back()
操作,然后用push_heap
将新增的元素加入堆中,再对元素进行pop_heap
后,元素被放到vector的最后一位,但是没有被删除,这个时候进行sort_heap()
会报错,需要手动对vector
进行pop_back()
。这个时候元素才算是真正的被删除。
完整代码如下:
#include
#include
#include
#include //greater()
using namespace std;
class Person
{
public:
Person(char *ch = "", int n = 0){
name = strdup(ch);
age = n;
}
bool operator==(const Person &p) const {
return strcmp(name, p.name) == 0;
}
bool operator<(const Person &p) const {
return strcmp(name, p.name) < 0;
}
private:
char *name;
int age;
friend ostream &operator<<(ostream &out, const Person &p){
out << "(" << p.name << "," << p.age << ")";
return out;
}
};
bool f1(int n)
{
return n < 5;
}
template<class T>
void printVector(char *s, const vector &v)
{
cout << s << " = (";
if(v.size() == 0){
cout << ")\n";
return ;
}
vector ::const_iterator i;
for(i = v.begin(); i != v.end() - 1; i++)
cout << *i << ',';
cout << *i << ")\n";
}
int main()
{
int a[] = {1,4,3,6,7,2,5};
vector<int> v1(a, a + 7), v2(a, a + 7), v3(6, 9), v4(6, 9);
vector<int>::iterator i1, i2, i3, i4;
partial_sort(v1.begin(), v1.begin() + 3, v1.end());
printVector("v1", v1);
partial_sort(v2.begin() + 1, v2.begin() + 4, v2.end(), greater<int>());
printVector("v2", v2);
i3 = partial_sort_copy(v2.begin(), v2.begin() + 4, v3.begin(), v3.end());
printVector("v3", v3);
cout << *(i3 - 1) << ' ' << *i3 << endl;
i4 = partial_sort_copy(v1.begin(), v1.begin() + 4, v4.begin(), v4.end(), greater<int>());
printVector("v4", v4);
cout << *(i4 - 1) << ' ' << *i4 << endl;
i1 = partition(v1.begin(), v1.end(), f1);
printVector("v1", v1);
cout << *(i1 - 1) << ' ' << *i1 << endl;
i2 = partition(v2.begin(), v2.end(), bind2nd(less<int>(), 5));
printVector("v2", v2);
cout << *(i2 - 1) << ' ' << *i2 << endl;
sort(v1.begin(), v1.end());
sort(v1.begin(), v1.end(), greater<int>());
vector pv1, pv2;
for(int i = 0; i < 20; i++){
pv1.push_back(Person("Josie", 60 - i));
pv2.push_back(Person("Josie", 60 - i));
}
sort(pv1.begin(), pv1.end());
stable_sort(pv2.begin(), pv2.end());
vector<int> heap1, heap2, heap3(a, a + 7), heap4(a, a + 7);
for(int i = 0; i < 7; i++){
heap1.push_back(i);
push_heap(heap1.begin(), heap1.end());
printVector("heap1", heap1);
}
sort_heap(heap1.begin(), heap1.end());
for(int i = 0; i < 7; i++){
heap2.push_back(i);
push_heap(heap2.begin(), heap2.end(), greater<int>());
printVector("heap2", heap2);
}
sort_heap(heap2.begin(), heap2.end(), greater<int>());
printVector("heap2", heap2);
make_heap(heap3.begin(),heap3.end());
sort_heap(heap3.begin(), heap3.end());
make_heap(heap4.begin(),heap4.end(), greater<int>());
printVector("heap4", heap4);
sort_heap(heap4.begin(),heap4.end(), greater<int>());
printVector("heap4", heap4);
return 0;
}
PS:比较函数
greater
//重载括号为比较大小
template <class T>
struct greater : binary_function bool>
{
bool operator() (const T& x, const T& y) const {return x>y;}
};
less
//重载括号为比较大小
template <class T>
struct less : binary_function bool>
{
bool operator() (const T& x, const T& y) const {return x