Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
appliWall
#include <iostream> #include <string> #include <sstream> #include <vector> #include <random> using namespace std; ostream& operator<<(ostream &str, const vector<int> & vect) { str << "\""; for(int i = 0; i < vect.size()-1; ++i) { str << vect[i] << ","; } str << vect.back() << "\"" << endl; return str; } // For each element , pick a random element to the left and swap them void myShuffle(vector<int> & appsVect) { random_device randDev; mt19937 engine{randDev()}; // mt19937 is a standard mersenne_twister_engine for (int i = 1; i < appsVect.size(); ++i) { uniform_int_distribution<int> dist(0, i-1); swap(appsVect[i], appsVect[dist(engine)]); } } int main() { string strIn; getline(cin, strIn); // remove the " if there are here if (strIn[0] == '\"') { strIn = strIn.substr(1, strIn.size()-2); } stringstream ss; ss << strIn; vector<int> appsVect; while (getline( ss, strIn, ',' )) { appsVect.push_back(stoi(strIn)); } int n = appsVect.size(); if (n == 0) { cout << "No elements to shuffle" << endl; return 1; } else if (n == 1) { cout << appsVect[0]; } else { myShuffle(appsVect); cout << appsVect; } return 0; }
run
|
edit
|
history
|
help
0
chakib
<string> No indirect include of <errno.h>
Tree Example
general
Zadanie Dejwu
10 naturalnumbers
Example Node Program
hw1 Os
Clang-IsBaseOf
Arduino Bluetooth Periodiek Systeem