C++ 算法库--排序

算法库 - cppreference.com

https://cplusplus.com/reference/algorithm/

Src: https://onlinegdb.com/ggGAUhV5n

/* 对一个vector中的元素进行多线程排序,不用开辟新的存储空间
*/

#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
#include <vector>
#include <array>
#include <span>
#include <memory>
#include <thread>
#include <map>
#include <mutex>
#include <condition_variable> //notify wait
#include <execution>

using namespace std;

using TBegin = uint32_t;
using TEnd   = uint32_t;
using TPair  = pair<TBegin,TEnd>;
using TBlock = uint32_t;

constexpr uint8_t KThreadMax=5;
std::mutex vMutex;

template<typename T>
void doSort(vector<T>& v,int start, int end){
    {
      lock_guard<mutex> _1(vMutex);//防止同时操作一个vector 出现意外
      sort(begin(v)+start, begin(v)+end,less<T>());  //merge/inplace_merge reuired that it must use less,not greater
    }

    cout<<"thread ID: "<<this_thread::get_id()<<" done!"<<endl;
}

template<typename T>
void doMerge(vector<T>& v,const map<TBlock,TPair>& mMap){
    for(const auto [id,p]:mMap) {
        auto & [b,e] = p;
        cout<<id<<" ["<<b<<" "<<e<<")"<<endl;
        
        if(id == 0) continue;
        inplace_merge(begin(v),begin(v)+b,begin(v)+e);
    }
}

int main() {
    vector<int> v={-999,1,13,4,-1,0,0,4,66,23,45,67,1,0,2,3,4,5,100,32,78,88,9,8,76,5,4,3,2,10000,5,6,7,8};
    const int blockSize = size(v)/KThreadMax;
    map<TBlock,TPair> mMap;
    vector<thread> threads;

    cout<<"vector size: "<<size(v)<<endl;
    cout<<"blockSize: "<<blockSize<<endl;

    for(int8_t i = 0; i < KThreadMax; ++i){
        int b = i*blockSize;
        int e = (i==KThreadMax-1) ? size(v) :(i+1)*blockSize; //last block may be more than blockSize

        mMap[i] = make_pair(b,e);
        threads.push_back(std::move(thread(doSort<int>,std::ref(v),b,e)));
    }

    for(auto & t : threads) {
        if(t.joinable()) t.join();
    }

    doMerge(v,mMap);

    cout<<"vector size: "<<size(v)<<endl;
    cout<<"is_sorted: "<<(is_sorted(begin(v),end(v))?"Yes":"No")<<endl;
    for_each(begin(v),end(v),[](const auto e){cout<<e<<" ";});


    cout<<endl;
    cout<<"reverse vector: "<<size(v)<<endl;
    reverse(begin(v),end(v));
    for_each(begin(v),end(v),[](const auto e){cout<<e<<" ";});

    // cout<<endl;
    // vector<int> sv {0,0,0};
    // cout<<( includes(begin(v),end(v),begin(sv),end(sv)) ? "include":"not include")<<endl;
}

 执行结果如下:

Src:https://onlinegdb.com/PvqcQ1kvJ

#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
#include <vector>

using namespace std;

// Helper to check for the existence of `begin()` and `end()` member functions.
template<typename T, typename = void>
struct has_begin_end : std::false_type {};

template<typename T>
struct has_begin_end<T, std::void_t<decltype(std::declval<T>().begin()), decltype(std::declval<T>().end())>> : std::true_type {};

template<typename T>
void show(const T& c){
    if constexpr(has_begin_end<T>()){
        if(!c.empty()) {
        copy(begin(c),end(c),ostream_iterator<typename T::value_type>(cout," "));
    }
    cout<<endl;
    }
}

int main()
{
    list<int> l1={3,2,1,5,4};
    list<int> l2={8,6,7,10,9};
    list<int> lo(size(l1)+size(l2));  //it must be ensured that contanier is enough space. 
    
    cout<<"l1.size = "<<l1.size()<<endl;
    cout<<"l2.size = "<<l2.size()<<endl;
    cout<<"lo.size = "<<lo.size()<<endl;
    
    cout<<"l1 unsorted list:";
    show(l1);
    
    /* random iterator use std::sort()*/
    // sort(begin(l1),end(l1),less<int>());
    // sort(begin(l2),end(l2),less<int>());

    /* list sort usage:< can't use std::sort() >*/
    l1.sort([](const int& e1, const int& e2){return e1<e2?true:false;});
    l2.sort([](const int& e1, const int& e2){return e1<e2?true:false;});
    cout<<"l1 sorted list:";
    show(l1);
    
    cout<<"l2 sorted list:";
    show(l2);
    
    l1.merge(l2); //std::list::merge()
    // std::merge(begin(l1),end(l1),begin(l2),end(l2),std::back_inserter(lo));  //container maybe not enough space if using back_inserter 
    // std::merge(begin(l1),end(l1),begin(l2),end(l2),begin(lo));  //lo mast have enough space
    cout<<"merged list:";
    show(l1);
    show(l2);
    show(lo);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值