STL算法之heap算法,已排序区间算法

// 1. sort() 将区间元素以谓词方式排序
// 2. stable_sort() sort()并保持区间元素的稳定性
// 3. partial_sort() 以谓词找到前[beg,mid)
// 4. partial_sort_copy() 将区间1的排序结果复制到区间2
// 5. nth_element() 将区间前n个元素排序
// 6. make_heap() 将区间元素转化成堆
// 7. push_heap() 加入新元素产生新堆
// 8. pop_heap() 将第一个元素移到尾部并将[beg,end-1)元素形成堆
// 9. sort_heap() 将堆进行排序(在原来排序准则下不再是堆)
// 10.binary_search() 判断有序区间是否存在value
// 11.includes() 判断有序区间是否是区间2子区间
// 12.lower_bound() 找出第一个大于等于value的元素
// upper_bound() 找出第一个大于value的元素
// equal_range() 找出等于value的范围[first,last)
// 13.merge() 将两个有序区间结合到另一序列
// 14.set_union() 找出两个有序区间的并集复制到另一序列
// 15.set_intersection() 找出两个有序区间的交集复制到另一序列
// 16.set_difference() 找出区间2对于区间1的差集并复制到另一序列
// 17.set_symmetric_difference() 找出区间2对于区间1的可重复差集并复制到另一序列
// 18.inplace_merge() 将[beg,end1) [end1,end] 结合

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
// 1. sort() 将区间元素以谓词方式排序
// 2. stable_sort() sort()并保持区间元素的稳定性
// 3. partial_sort() 以谓词找到前[beg,mid)
// 4. partial_sort_copy() 将区间1的排序结果复制到区间2
// 5. nth_element() 将区间前n个元素排序
// 6. make_heap() 将区间元素转化成堆
// 7. push_heap() 加入新元素产生新堆
// 8. pop_heap() 将第一个元素移到尾部并将[beg,end-1)元素形成堆
// 9. sort_heap() 将堆进行排序(在原来排序准则下不再是堆)
// 10.binary_search() 判断有序区间是否存在value
// 11.includes() 判断有序区间是否是区间2子区间
// 12.lower_bound() 找出第一个大于等于value的元素
//    upper_bound() 找出第一个大于value的元素
//    equal_range() 找出等于value的范围[first,last)
// 13.merge()  将两个有序区间结合到另一序列
// 14.set_union() 找出两个有序区间的并集复制到另一序列
// 15.set_intersection() 找出两个有序区间的交集复制到另一序列
// 16.set_difference() 找出区间2对于区间1的差集并复制到另一序列
// 17.set_symmetric_difference() 找出区间2对于区间1的可重复差集并复制到另一序列
// 18.inplace_merge() 将[beg,end1) [end1,end] 结合
void test()
{
    //1.
    vector<int> a{1,2,3,4,5,6,7,8,9 };
    sort(a.begin(), a.end(), greater<>());
    for (auto &v : a)
        cout << v << ends;
    cout << endl;
    vector<int> b{ 1,2,3,4,5,5,5,6,7,8 };
    //2
    stable_sort(b.begin(), b.end(), greater<>());
    for (auto &v : b)
        cout << v << ends;
    cout << endl;
    //3
    vector<int> c{ 2,2,3,4,5,6,5,4 };
    partial_sort(c.begin(),c.begin() + 4,c.end(),greater<>());
    for (auto &v : c)
        cout << v << ends;
    cout << endl;
    //4
    vector<int> d(b.size());
    partial_sort_copy(b.begin(), b.end(),d.begin(),d.end(),less<int>());
    for (auto &v : d)
        cout << v << ends;
    cout << endl;
    vector<int> e{ 1,2,3,6,4,5,9,0 };
    //5
    nth_element(e.begin(), e.begin() + 4, e.end(), greater<>());
    for (auto &v : e)
        cout << v << ends;
    cout << endl;
    //6
    vector<int> f{ 1,5,8,9,0,2,3 };
    make_heap(f.begin(), f.end(),greater<int>());
    for (auto &v : f)
        cout << v << ends;
    //7
    pop_heap(f.begin(), f.end(),greater<>());
    cout << endl;
    f.pop_back();
    for (auto &v : f)
        cout << v << ends;
    f.push_back(11);
    //8
    push_heap(f.begin(), f.end(),greater<>());
    for (auto &v : f)
        cout << v << ends;
    //9
    sort_heap(f.begin(), f.end(),greater<>());
    for (auto &v : f)
        cout << v << ends;
    cout << endl;
    if (is_heap(f.begin(), f.end(),greater<>()))
        cout << "f is delelop heap" << endl;
    else
        cout << "f is not develop heap" << endl;
    vector<int> g{1,2,3,4,5,6,7,8,9 };
    //10
    if (binary_search(g.begin(), g.end(), 5))
        cout << "5 is exist in g" << endl;
    else
        cout << "5 is not exist in g" << endl;
    //11
    vector<int> m{ 4,5,6};
    if (includes(g.begin(), g.end(), m.begin(), m.end()))
        cout << "m includes in g" << endl;
    else
        cout << "m not includes in g" << endl;
    vector<int> n{ 1,2,3,4,5,6,7,8,9};
    //12
    auto fp = lower_bound(n.begin(), n.end(), 5);
    auto sp = upper_bound(n.begin(), n.end(), 5);
    cout << "n has how many 5 " << distance(fp, sp) << endl;
    auto ppos = equal_range(n.begin(), n.end(), 5);
    cout << "n has how many 5 " << distance(ppos.first, ppos.second) << endl;
    vector<int> j;
    //13
    merge(m.begin(), m.end(), n.begin(), n.end(), back_inserter(j),less<>());
    for (auto &v : j)
        cout << v << ends;
    cout << endl;
    //14
    set_union(m.begin(), m.end(), n.begin(), n.end(), back_inserter(j));
    for (auto &v : j)
        cout << v << ends;
    cout << endl;
    vector<int> i;
    //15
    set_intersection(m.begin(), m.end(), n.begin(), n.end(), back_inserter(i));
    for (auto &v : i)
        cout << v << ends;
    cout << endl;
    i.resize(0);
    //16
    set_difference(n.begin(), n.end(), m.begin(), m.end(),back_inserter(i));
    for (auto &v : i)
        cout << v << ends;
    i.resize(0);
    cout << endl;
    //17
    set_symmetric_difference(m.begin(), m.end(), n.begin(), n.end(), back_inserter(i));
    for (auto &v : i)
        cout << v << ends;
    cout << endl;
    i.resize(0);
    i.assign({ 1,2,3,4,5,6,7,8,1,2,3,4,5 });
    auto pos = find(i.begin(), i.end(), 8);
    //18
    inplace_merge(i.begin(), ++pos, i.end());
    for (auto &v : i)
        cout << v << ends;
}
int main()
{
    test();
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值