Skip to main content
GameDev.net gamedev.net
🔒 Locked

Random order

Started by Austrian Coder May 3, 2003 at 8:55 AM 6 replies 500+ views
Original Post
Austrian Coder
Austrian Coder
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
Enselic
Enselic
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]
Austrian Coder
Austrian Coder
Yeah... i only have the problem with spiltting the 4 parts. Oh and this function should work with n parts.
Enselic
Enselic
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.
civguy
civguy
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]
Austrian Coder
Austrian Coder
It is no homework. I dont have computer/software development in school. It is for a projekt i am working at.
civguy
civguy
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)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.