Hello, I need to created text file with 100,000 reverse sorted out elements. I generated my random 100,000 elements from website, how do I get the reverse sorted out version???
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <functional>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int WIDTH = 3;
//======================================================================
template <typename T> ostream &operator << ( ostream &strm, vector<T> &M ) // Output a vector<T>
{
for ( auto e : M ) strm << setw( WIDTH ) << e << ' ';
return strm;
}
//======================================================================
vector<int> getNumbers( unsignedint N, int a, int b ) // Return N random numbers between a and b (inclusive)
{
vector<int> result;
int d = b - a + 1;
while ( N-- ) result.push_back( a + rand() % d );
return result;
}
//======================================================================
int main()
{
srand( time( 0 ) );
vector<int> V = getNumbers( 10, 0, 99 ); // Get some numbers
cout << "Original: " << V << '\n';
sort( V.begin(), V.end(), greater<int>() ); // Sort by predicate
cout << "Reverse sorted: " << V << '\n';
// Write to file (one per line)
ofstream out( "output.txt" );
for ( int e : V ) out << e << '\n';
}