// ==================================
std::string RandomOrder(std::string Input)
{
//string input = "1A 2B 3C 4D";
std::stringstream ss(Input);
std::stringstream result_ss;
std::vector<std::string> tokens;
while (ss.good())
{
std::string text;
ss >> text;
tokens.push_back(text);
}
std::random_shuffle(tokens.begin(), tokens.end());
std::copy(tokens.begin(), tokens.end(), std::ostream_iterator<std::string>(result_ss, " "));
std::string Ausgabe; = result_ss.str();
return Ausgabe;
}
But i have one problem with it. There once there are two spaces in the output (1A 4D 3C 2B). How can i fix this?
Thanks, Christian
Random order
Hello!
I have written an routine to random the order of strings.
Look here
input: 1A 2B 3C 4D
output: 1A 4D 3C 2B
Here is the function
I can''t see the problem. But here''s a suggestion for compacter definition.
By the way, it will always have an extra space at the end.
std::string RandomOrder(std::string Input){ using namespace std; stringstream ss(Input); stringstream result_ss; istream_iterator<string> beg(ss), end; vector<string> tokens(beg, end); random_shuffle(tokens.begin(), tokens.end()); copy(tokens.begin(), tokens.end(), ostream_iterator<string>(result_ss, " ")); return result_ss.str();}
By the way, it will always have an extra space at the end.
"1A 4D 3C 2B "
By the way, here''s a useful #define:
It allows to shorten a lot of your code.
Makes it more readable too.
#define ALL(__xix__) __xix__.begin(), __xix__.end()
It allows to shorten a lot of your code.
random_shuffle(ALL(tokens));
Makes it more readable too.
The overloaded version of
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[edited by - Lektrix on August 17, 2003 1:38:41 PM]
std::random_shuffle()
that you are using uses an internal random number generator, and it may not be very random. Make a small program to exercise the function and you will see that the "random" numbers will be the same every time. The other overloaded version, though, allows you to pass a random number generator in the form of a function object. I assume from the documentation that you would do something like this, yet I'm not too sure:class RandNumGen{public: RandNumGen() { srand( unsigned ( time(0) ) ); // shouldn't really be here, as it is called every time an object is constructed } int operator()(int N) { return rand() % N; }}; ... std::string RandomOrder( const std::string& input ); ... std::string RandomOrder( const std::string& input ){ std::stringstream ss(input); std::istream_iterator<std::string> beg(ss), end; std::vector<std::string> tokens( beg, end ); RandNumGen rng; std::random_shuffle( tokens.begin(), tokens.end(), rng ); std::stringstream result_ss; std::copy( tokens.begin(), tokens.end(), std::ostream_iterator<std::string>( result_ss, " " ) ); // if you want to remove the last character (space) std::string result( result_ss.str() ); result.erase( result.end() - 1 ); // hax :/ return result; // else you would merely use: `return result_ss.str();'}
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[edited by - Lektrix on August 17, 2003 1:38:41 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement