前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >STL算法(排序)简介mergesortrandom_shufflereverse

STL算法(排序)简介mergesortrandom_shufflereverse

作者头像
用户2929716
发布2018-08-23 13:21:35
2610
发布2018-08-23 13:21:35
举报
文章被收录于专栏:流媒体流媒体

简介

merge sort random_shuffle reverse

merge

合并两个有序序列,存放到另一个序列。函数定义:

 #if _ITERATOR_DEBUG_ARRAY_OVERLOADS
template<class _InIt1,
    class _InIt2,
    class _OutTy,
    size_t _OutSize,
    class _Pr> inline
    _OutTy *merge(_InIt1 _First1, _InIt1 _Last1,
        _InIt2 _First2, _InIt2 _Last2,
        _OutTy (&_Dest)[_OutSize], _Pr _Pred)
    {   // copy merging ranges, both using _Pred, array dest
    return (_Unchecked(
        _Merge_no_deprecate(_First1, _Last1,
            _First2, _Last2,
            _Array_iterator<_OutTy, _OutSize>(_Dest), _Pred)));
    }
 #endif /* _ITERATOR_DEBUG_ARRAY_OVERLOADS */

        // TEMPLATE FUNCTION merge
template<class _InIt1,
    class _InIt2,
    class _OutIt> inline
    _OutIt merge(_InIt1 _First1, _InIt1 _Last1,
        _InIt2 _First2, _InIt2 _Last2,
        _OutIt _Dest)
    {   // copy merging ranges, both using operator<
    return (_STD merge(_First1, _Last1, _First2, _Last2, _Dest,
        less<>()));
    }

sort

以默认升序的方式重新排列指定范围内的元素。若要改排序规则,可以输入比较函数。

template<class _RanIt,
    class _Pr> inline
    void sort(_RanIt _First, _RanIt _Last, _Pr _Pred)
    {   // order [_First, _Last), using _Pred
    _DEBUG_RANGE(_First, _Last);
    _Sort_unchecked(_Unchecked(_First), _Unchecked(_Last), _Pred);
    }

        // TEMPLATE FUNCTION sort
template<class _RanIt> inline
    void sort(_RanIt _First, _RanIt _Last)
    {   // order [_First, _Last), using operator<
    _STD sort(_First, _Last, less<>());
    }

random_shuffle

对指定范围内的元素随机调整次序 srand(time(0)); //设置随机种子

template<class _RanIt> inline
    void random_shuffle(_RanIt _First, _RanIt _Last)
    {   // shuffle [_First, _Last) using rand()
    _Rand_urng_from_func _Func;
    _STD shuffle(_First, _Last, _Func);
    }

reverse

对指定范围内的元素逆序

template<class _BidIt> inline
    void reverse(_BidIt _First, _BidIt _Last)
    {   // reverse elements in [_First, _Last)
    _DEBUG_RANGE(_First, _Last);
    _Reverse_unchecked(_Unchecked(_First), _Unchecked(_Last));
    }

示例代码

#include "stdafx.h"
#include "stdafx.h"
#include "iostream"
#include "string"
#include "algorithm"
#include "vector"
#include "list"
#include <functional>
using namespace std;
class Student {
private:
    int number;
    string name;
public:
    //Student() {
    //  this->number = -1;
    //  this->name = "";
    //}
    Student(int number, string name) {
        cout << "构造 " << number << " " << name.c_str() << endl;
        this->number = number;
        this->name = name;
    }
    Student(const Student & stu) {
        //cout << "copy构造" <<stu.getNumber()<<" "<<stu.getName().c_str()<< endl;
        this->number = stu.getNumber();
        this->name = stu.getName();
    }
    ~Student() {
        //cout<<"析构 " << this->number << " " << this->name.c_str() << endl;
    }

    Student& operator=(const Student& stu) {
        this->number = stu.getNumber();
        this->name = stu.getName();
        return *this;
    }

    void print()const {
        cout << "print 》》 " << this->number << " " << this->name.c_str() << endl;
    }

    int getNumber() const {
        return this->number;
    }
    string getName()const {
        return this->name;
    }
};

struct StuFunc
{
    bool operator()(const Student & stu1, const Student & stu2) const {
        cout << "StuFunc》》" << endl;
        stu1.print();
        stu2.print();
        return stu1.getNumber() <stu2.getNumber();
    }
};

void printStuV(vector<Student> v) {
    cout << "开始遍历vector<Student>============" << endl;
    for (vector<Student>::iterator it = v.begin(); it != v.end(); it++) {
        it->print();
    }
    cout << "结束遍历vector<Student>============" << endl;
}
void printNum(vector<int>v) {
    cout << "开始遍历vector<int>============" << endl;
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
    cout << "结束遍历vector<int>============" << endl;
}

int main()
{
    //merge示例
    vector<int> vNum;
    vNum.push_back(1);
    vNum.push_back(3);
    vNum.push_back(5);
    vector<int> vNum2;
    vNum2.push_back(2);
    vNum2.push_back(4);
    vector<int> vNum3;
    vNum3.resize(vNum.size() + vNum2.size());
    merge(vNum.begin(), vNum.end(), vNum2.begin(), vNum2.end(), vNum3.begin());
    printNum(vNum3);

    vector<Student> v;
    v.push_back(Student(1, "one"));
    Student stu2(2, "two");
    v.push_back(stu2);
    v.push_back(Student(4, "four"));
    v.push_back(Student(3, "three"));
    v.push_back(Student(5, "five"));
    cout << "sort 随机排序" << endl;
    sort(v.begin(), v.end(), StuFunc());
    printStuV(v);
    cout << "random_shuffle 随机排序" << endl;
    random_shuffle(v.begin(), v.end());
    printStuV(v);
    cout << "reverse 逆序" << endl;
    reverse(v.begin(), v.end());
    printStuV(v);
    return 0;
}

结果:

sort

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.09.04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
  • merge
  • sort
  • random_shuffle
  • reverse
    • 示例代码
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档