• C++ populate template array via random generator and finally sort,print


    #pragma once
    #pragma comment(lib,"rpcrt4.lib")
    #include <algorithm>
    #include <cstring>
    #include <iostream>
    #include <random>
    #include <vector>
    #include <Windows.h>
    
    using namespace std;
    
    
    static random_device rd;
    static mt19937_64 mt{ rd() };
    
    template<typename T>
    T getRandom(T min, T max)
    {
        uniform_int_distribution<T> uid(min, max);
        return uid(mt);
    }
    
    template<typename T>
    void getArray(T* arr, T min, T max,int len)
    {
        for (int i = 0; i < len; i++)
        {
            arr[i] = getRandom<T>(min, max);
        }
    }
    
    template<typename T>
    void printArray(T* arr, int len)
    {
        for (int i = 0; i < len; i++)
        {
            cout << arr[i] << "\t";
        }
        cout << endl << endl << endl;
    }
    
    void arraySortDemo(int len)
    {
        uint32_t* arr = new uint32_t[len];
        getArray<uint32_t>(arr, 0, UINT32_MAX,len);
        cout << "Before sort:" << endl;
        printArray(arr, len);
        cout << "After sort:" << endl;
        std::sort(arr, arr + len,std::greater<uint32_t>());
        printArray(arr, len);
        delete[] arr;
        cout << endl << endl << endl;
    }
    
    int main(int args, char** argv)
    {
        arraySortDemo(100);
    }

  • 相关阅读:
    mogodb优化
    uuid
    ssl详解
    探究rh6上mysql5.6的主从、半同步、GTID多线程、SSL认证主从复制
    CMAKE MYSQL
    证书生成
    叶金荣主页
    mysqlslap
    sysbench 测试MYSQL
    mysql实验室
  • 原文地址:https://www.cnblogs.com/Fred1987/p/16709849.html
Copyright © 2020-2023  润新知