• Advertisement

Archived

This topic is now archived and is closed to further replies.

Random order

This topic is 5376 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites

  • Advertisement