Random order

Started by
6 comments, last by Austrian Coder 20 years, 11 months ago
Hi! I have an std::string "1A 2B 3C 4D". And now i want to make out of it a random ordered std::string, e.g., "3C 1A 4D 2B". But i dont know how to make it. Can somebody help me? Thanks, Christian
Advertisement
Split it up into four strings and randomly merge them together into a new one.


____________________ ____ ___ __ _
Enselic's Corner - My site. Go test my game Spatra and see if you can beat it onto the Official Spatra Top 10.

CodeSampler.com - Great site with source for specific tasks in DirectX and OpenGL.

[edited by - Enselic on May 3, 2003 10:01:11 AM]
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
Yeah... i only have the problem with spiltting the 4 parts. Oh and this function should work with n parts.
I''ve never coded with the std::string but:
for n pairs  string n = base_string( char n * 3 to char n * 3 + 2 ) 



____________________ ____ ___ __ _
Enselic''s Corner - My site. Go test my game Spatra and see if you can beat it onto the Official Spatra Top 10.
CodeSampler.com - Great site with source for specific tasks in DirectX and OpenGL.
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
edit: in the joy of creation, I didn't think this could be a homework question. Solution removed.

Hint: use stringstream to split the string into smaller strings (it splits on whitespace)

[edited by - civguy on May 3, 2003 10:29:05 AM]
It is no homework. I dont have computer/software development in school. It is for a projekt i am working at.
Well, I hope this helps then:

      int main() {  using namespace std;  string input = "1A 2B 3C 4D";  stringstream ss(input);  vector<string> tokens;  while (ss.good()) {    string text;    ss >> text;    tokens.push_back(text);  }  random_shuffle(tokens.begin(), tokens.end());  stringstream result_ss;  copy(tokens.begin(), tokens.end(), ostream_iterator<string>(result_ss, " "));  string output = result_ss.str();}      
(untested)
Thanks...

This topic is closed to new replies.

Advertisement