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