pointer_to_unary_function
Converts a unary(一元的) function pointer into an adaptable(适合的) unary function.
template<class Arg, class Result> class pointer_to_unary_function : public unary_function<Arg, Result> { public: explicit pointer_to_unary_function(Result ( *_pfunc)(Arg) ); Result operator()( const Arg _Left ) const; };
Parameters
-
_pfunc
-
The binary function to be converted.
-
_Left
-
The object that the *_pfunc is called on.
The template class stores a copy of
_pfunc
. It defines its member function
operator()
as returning (*
_pfunc
)(_
Left
).
A unary function pointer is a function object and may be passed to any Standard Template Library algorithm that is expecting a unary function as a parameter, but it is not adaptable. To use it with an adaptor, such as binding a value to it or using it with a negator, it must be supplied with the nested types argument_type and result_type that make such an adaptation possible. The conversion by pointer_to_unary_function allows the function adaptors to work with binary function pointers.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm> /need random_shuffle()
#include <vector>
#include <functional> need ptr_fun
using namespace std;
int iarray[10]={1,2,3,4,5,6,7,8,9,10};
vector<int> v(iarray,iarray+10);
void display(vector<int>& vr,const char *s);
unsigned int randInt(unsigned int n);
int main()
{
srand(time(NULL));
display(v,"before shuffle: ");
pointer_to_unary_function<unsigned int,unsigned int> ptr_randInt=ptr_fun(randInt);
random_shuffle(v.begin(),v.end(),ptr_randInt);
display(v,"after shuffle: ");
return 0;
}
void display(vector<int>& vr,const char* s)
{
cout<<endl<< s <<endl;
copy(vr.begin(),vr.end(),ostream_iterator<int>(cout," "));
}
unsigned int randInt(unsigned int n)
{
return rand()%n;
}
/*-----------------------------------
before shuffle:
1 2 3 4 5 6 7 8 9 10
after shuffle:
5 3 9 6 4 7 8 10 1 2 Press any key to continue
*--------------------------------------*/
Randomly rearrange elements in range
Rearranges the elements in the range
[first,last)
randomly.
The function swaps the value of each element with that of some other randomly picked element. When provided, the function gen determines which element is picked in every case. Otherwise, the function uses some unspecified source of randomness.
To specify a uniform random generator as those defined in
<random>
, see
shuffle.
The behavior of this function template (2) is equivalent to:
| |
Parameters
-
first, last
-
Random-access iterators to the initial and final positions of the sequence to be shuffled. The range used is
[first,last)
, which contains all the elements between first and last, including the element pointed by firstbut not the element pointed by last.
gen
-
Unary function taking one argument and returning a value, both convertible to/from the appropriate
difference type used by the iterators. The function shall return a non-negative value less than its argument.
This can either be a function pointer or a function object.
RandomAccessIterator shall point to a type for which swap is defined and swaps the value of its arguments.
#include <iostream>
#include <algorithm> ///std::random_shuffle
#include <vector>
#include <ctime>
#include <cstdlib> ///std::rand,std::srand
#include <functional> ///need ptr_fun()
using namespace std;
int myrandom(int i)
{
return rand()%i;
}
int main()
{
srand(time(0));
vector<int> myvector;
pointer_to_unary_function<int,int> ptr_myrandom=ptr_fun(myrandom);
///set some values
for(int i=1; i<10; ++i)
myvector.push_back(i);
random_shuffle(myvector.begin(),myvector.end());
cout<<"myvector contains:";
for(vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
cout<<' '<<*it;
cout<<endl;
random_shuffle(myvector.begin(),myvector.end(),ptr_myrandom);
cout<<"myvector contains(using myrandom):";
for(it=myvector.begin(); it!=myvector.end(); ++it)
cout<<' '<<*it;
cout<<endl;
return 0;
}
/**--------------
myvector contains: 6 1 8 7 2 3 9 4 5
myvector contains(using myrandom): 9 4 7 2 1 5 3 6 8
Press any key to continue
-------------------------*/