发生器函数对象

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.


Header:  <functional >
#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:
1
2
3
4
5
6
7
8
9
10
template <class RandomAccessIterator, class RandomNumberGenerator>
  void random_shuffle (RandomAccessIterator first, RandomAccessIterator last,
                       RandomNumberGenerator& gen)
{
  iterator_traits<RandomAccessIterator>::difference_type i, n;
  n = (last-first);
  for (i=n-1; i>0; --i) {
    swap (first[i],first[gen(i+1)]);
  }
}

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
-------------------------*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值