C++/STL Algorithms Modifying sequence operations/random shuffle
Содержание
- 1 Randomize elements in a vector with random_shuffle(scores.begin(), scores.end());
- 2 std::random_shuffle a vector
- 3 Use back_insert_iterator to insert element into a vector
- 4 Use random_shuffle to shuffle all elements randomly
- 5 Use random_shuffle to shuffle elements with self-written random number generator
Randomize elements in a vector with random_shuffle(scores.begin(), scores.end());
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
vector<int>::const_iterator iter;
vector<int> scores;
scores.push_back(1);
scores.push_back(3);
scores.push_back(5);
for (iter = scores.begin(); iter != scores.end(); ++iter)
cout << *iter << endl;
srand(time(0));
random_shuffle(scores.begin(), scores.end());
for (iter = scores.begin(); iter != scores.end(); ++iter)
cout << *iter << endl;
return 0;
}
std::random_shuffle a vector
#include <iostream>
using std::cout;
using std::endl;
#include <algorithm>
#include <numeric>
#include <vector>
#include <iterator>
int main()
{
int a1[ 10 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::vector< int > v( a1, a1 + 10 ); // copy of a1
std::ostream_iterator< int > output( cout, " " );
cout << "Vector v before random_shuffle: ";
std::copy( v.begin(), v.end(), output );
std::random_shuffle( v.begin(), v.end() ); // shuffle elements of v
cout << "\nVector v after random_shuffle: ";
std::copy( v.begin(), v.end(), output );
cout << endl;
return 0;
}
/*
Vector v before random_shuffle: 1 2 3 4 5 6 7 8 9 10
Vector v after random_shuffle: 9 2 10 3 1 6 8 4 5 7
*/
Use back_insert_iterator to insert element into a vector
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main( ) {
vector<int> v;
back_insert_iterator<std::vector<int> > p = back_inserter(v);
for (int i = 0; i < 10; ++i)
*p = i;
for (int i = 0; i < 10; ++i){
cout << v[i];
}
cout << "\n\n\n";
random_shuffle(v.begin( ), v.end( ));
for (int i = 0; i < 10; ++i){
cout << v[i];
}
}
/*
0123456789
8192057346
*/
Use random_shuffle to shuffle all elements randomly
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <cstdlib>
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <numeric>
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by
* - all elements of the collection coll
* - separated by spaces
*/
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
typename T::const_iterator pos;
std::cout << optcstr;
for (pos=coll.begin(); pos!=coll.end(); ++pos) {
std::cout << *pos << " ";
}
std::cout << std::endl;
}
/* INSERT_ELEMENTS (collection, first, last)
* - fill values from first to last into the collection
* - NOTE: NO half-open range
*/
template <class T>
inline void INSERT_ELEMENTS (T& coll, int first, int last)
{
for (int i=first; i<=last; ++i) {
coll.insert(coll.end(),i);
}
}
using namespace std;
class MyRandom {
public:
ptrdiff_t operator() (ptrdiff_t max) {
double tmp;
tmp = static_cast<double>(rand())
/ static_cast<double>(RAND_MAX);
return static_cast<ptrdiff_t>(tmp * max);
}
};
int main()
{
vector<int> coll;
INSERT_ELEMENTS(coll,1,9);
PRINT_ELEMENTS(coll,"coll: ");
// sort them again
sort (coll.begin(), coll.end());
PRINT_ELEMENTS(coll,"sorted: ");
// shuffle all elements randomly
random_shuffle (coll.begin(), coll.end());
PRINT_ELEMENTS(coll,"shuffled: ");
}
/*
coll: 1 2 3 4 5 6 7 8 9
sorted: 1 2 3 4 5 6 7 8 9
shuffled: 9 2 7 3 1 6 8 4 5
*/
Use random_shuffle to shuffle elements with self-written random number generator
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <cstdlib>
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <numeric>
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by
* - all elements of the collection coll
* - separated by spaces
*/
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
typename T::const_iterator pos;
std::cout << optcstr;
for (pos=coll.begin(); pos!=coll.end(); ++pos) {
std::cout << *pos << " ";
}
std::cout << std::endl;
}
/* INSERT_ELEMENTS (collection, first, last)
* - fill values from first to last into the collection
* - NOTE: NO half-open range
*/
template <class T>
inline void INSERT_ELEMENTS (T& coll, int first, int last)
{
for (int i=first; i<=last; ++i) {
coll.insert(coll.end(),i);
}
}
using namespace std;
class MyRandom {
public:
ptrdiff_t operator() (ptrdiff_t max) {
double tmp;
tmp = static_cast<double>(rand())
/ static_cast<double>(RAND_MAX);
return static_cast<ptrdiff_t>(tmp * max);
}
};
int main()
{
vector<int> coll;
INSERT_ELEMENTS(coll,1,9);
PRINT_ELEMENTS(coll,"coll: ");
// sort them again
sort (coll.begin(), coll.end());
PRINT_ELEMENTS(coll,"sorted: ");
/* shuffle elements with self-written random number generator
* - to pass an lvalue we have to use a temporary object
*/
MyRandom rd;
random_shuffle (coll.begin(), coll.end(), // range
rd); // random number generator
PRINT_ELEMENTS(coll,"shuffled: ");
}
/*
coll: 1 2 3 4 5 6 7 8 9
sorted: 1 2 3 4 5 6 7 8 9
shuffled: 4 3 8 7 5 2 6 1 9
*/