首页 > 编程语言 >C++ populate template array via random generator and finally sort,print

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

时间:2022-09-20 09:24:06浏览次数:61  
标签:sort arr via generator int max len template include

#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);
}

 

 

标签:sort,arr,via,generator,int,max,len,template,include
From: https://www.cnblogs.com/Fred1987/p/16709849.html

相关文章

  • js数组sort()方法按指定顺序排序
    一、sort介绍数组的sort()方法可以把数组排序,不传参数的默认按字典排序sort()方法还接受一个回调函数,按回调函数内代码逻辑排序该函数要比较两个值,然后返回一个用于说明这......
  • Insertion or Heap Sort (25)
    题目描述AccordingtoWikipedia:Insertionsortiterates,consumingoneinputelementeachrepetition,andgrowingasortedoutputlist.Eachiteration,inserti......
  • js四种异步方法(回调函数、Promise、Generator、async/await)
    由于JS运行环境是单线程的,即一次只能完成一个任务,所以多任务时需要排队。异步可以理解为改变执行顺序的操作,异步任务必须在同步任务执行结束之后,从任务队列中依次取出执行......
  • Application of wireless technology in aviation IPQ6018/IPQ6000/IPQ6010/Wallys
    ApplicationofwirelesstechnologyinaviationIPQ6018/IPQ6000/IPQ6010/WallysThisarticlediscussessomekeyrecentdevelopmentsintheareaofwirelessnetwo......
  • Vue sortable实现排序功能
     1.vue代码 <template><el-table@selection-change="handleSelectionChange"@sort-change="sortChange"v-loading="loading"id="TableColumnID"eleme......
  • vc++ get random via random_device,mt19937
     #include<ctime>#include<iostream>#include<random>usingnamespacestd;staticrandom_devicerd;staticmt19937mt{rd()};template<typenameT>vo......
  • mybatis-plus-generator代码生成器(新)
    代码生成器所需要的依赖<!--代码生成器--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</ar......
  • Python 中的 sorted 和 sort的区别
    Python中的sorted和sort的区别#sort与sorted区别:#sorted()是内置函数.sorted可以对所有可迭代的对象进行排序操作,有返回值,返回列表;#sort是list上的方法,是对......
  • 【python】sort 排序
    sort排序fromoperatorimportitemgettera=[ {'name':'小张','create_time':'2020-10-1609:56'}, {'name':'小王','create_time':'2020-10-1609:57'}, {'name'......
  • Collections.sort排序方法的最简化写法
    假定按照Number对象的Id字段进行排序正序排序Collections.sort(resultList,Comparator.comparing(Number::getId));逆序排序Collections.sort(resultList,Comparato......