#include <iostream.h>
#include <stdlib.h> // Need random(), srandom()#include <time.h> // Need time()#include <vector> // Need vector#include <algorithm> // Need for_each()#define VSIZE 24 // Size of vectorvector<long> v(VSIZE); // Vector object
// Function prototypes
void initialize(long &ri);
void show(const long &ri);
bool isMinus(const long &ri); // Predicate function
int main()
{srandom( time(NULL) ); // Seed random generator
for_each(v.begin(), v.end(), initialize);//调用普通函数
cout << "Vector of signed long integers" << endl;
for_each(v.begin(), v.end(), show);
cout << endl;
// Use predicate function to count negative values
//
int count = 0;
vector<long>::iterator p;
p = find_if(v.begin(), v.end(), isMinus);//调用断言函数
while (p != v.end()) { count++; p = find_if(p + 1, v.end(), isMinus);}
cout << "Number of values: " << VSIZE << endl;
cout << "Negative values : " << count << endl;
return 0;
}
// Set ri to a signed integer value
void initialize(long &ri)
{ri = ( random() - (RAND_MAX / 2) );
// ri = random();
}
// Display value of ri
void show(const long &ri)
{cout << ri << " ";
}
// Returns true if ri is less than 0
bool isMinus(const long &ri)
{return (ri < 0);
}
TAnyClass object; // Construct object
object(); // Calls TAnyClass::operator()() functionfor_each(v.begin(), v.end(), object);
#include <iostream.h>
#include <numeric> // Need accumulate()#include <vector> // Need vector#include <functional> // Need multiplies() (or times())#define MAX 10
vector<long> v(MAX); // Vector objectint main()
{// Fill vector using conventional loop
//
for (int i = 0; i < MAX; i++)
v[i] = i + 1;// Accumulate the sum of contained values
//
long sum =
accumulate(v.begin(), v.end(), 0);cout << "Sum of values == " << sum << endl;
// Accumulate the product of contained values
//
long product =
accumulate(v.begin(), v.end(), 1, multiplies<long>());//注意这行cout << "Product of values == " << product << endl;
return 0;
}
$ g++ accum.cpp
$ ./a.out
Sum of values == 55
Product of values == 3628800
#include <iostream.h>
#include <stdlib.h> // Need random(), srandom()#include <time.h> // Need time()#include <algorithm> // Need random_shuffle()#include <vector> // Need vector#include <functional> // Need ptr_fun()
using namespace std;
// Data to randomize
int iarray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};vector<int> v(iarray, iarray + 10);
// Function prototypes
void Display(vector<int>& vr, const char *s);
unsigned int RandInt(const unsigned int n);
int main()
{srandom( time(NULL) ); // Seed random generator
Display(v, "Before shuffle:");
pointer_to_unary_function<unsigned int, unsigned int>
ptr_RandInt = ptr_fun(RandInt); // Pointer to RandInt()//注意这行random_shuffle(v.begin(), v.end(), ptr_RandInt);
Display(v, "After shuffle:");
return 0;
}
// Display contents of vector vr
void Display(vector<int>& vr, const char *s)
{cout << endl << s << endl;
copy(vr.begin(), vr.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
// Return next random value in sequence modulo n
unsigned int RandInt(const unsigned int n)
{return random() % n;
}
$ g++ randfunc.cpp
$ ./a.out
Before shuffle:
1 2 3 4 5 6 7 8 9 10
After shuffle:
6 7 2 8 3 5 10 1 9 4
pointer_to_unary_function<unsigned int, unsigned int>
ptr_RandInt = ptr_fun(RandInt);
random_shuffle(v.begin(), v.end(), ptr_RandInt);
在本例子中,发生器只是简单的调用rand()函数。
#include <iostream.h>
#include <algorithm> // Need random_shuffle()#include <vector> // Need vector#include <functional> // Need unary_function
using namespace std;
// Data to randomize
int iarray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};vector<int> v(iarray, iarray + 10);
// Function prototype
void Display(vector<int>& vr, const char *s);
// The FiboRand template function-object class
template <class Arg>
class FiboRand : public unary_function<Arg, Arg> {int i, j;
Arg sequence[18];
public:
FiboRand();
Arg operator()(const Arg& arg);
};
void main()
{FiboRand<int> fibogen; // Construct generator object
cout << "Fibonacci random number generator" << endl;
cout << "using random_shuffle and a function object" << endl;
Display(v, "Before shuffle:");
random_shuffle(v.begin(), v.end(), fibogen);
Display(v, "After shuffle:");
}
// Display contents of vector vr
void Display(vector<int>& vr, const char *s)
{cout << endl << s << endl;
copy(vr.begin(), vr.end(),
ostream_iterator<int>(cout, " "));cout << endl;
}
// FiboRand class constructor
template<class Arg>
FiboRand<Arg>::FiboRand()
{sequence[17] = 1;
sequence[16] = 2;
for (int n = 15; n > 0; n—)
sequence[n] = sequence[n + 1] + sequence[n + 2];i = 17;
j = 5;
}
// FiboRand class function operator
template<class Arg>
Arg FiboRand<Arg>::operator()(const Arg& arg)
{Arg k = sequence[i] + sequence[j];
sequence[i] = k;
i--;
j--;
if (i == 0) i = 17;
if (j == 0) j = 17;
return k % arg;
}
$ g++ fiborand.cpp
$ ./a.out
Fibonacci random number generator
using random_shuffle and a function object
Before shuffle:
1 2 3 4 5 6 7 8 9 10
After shuffle:
6 8 5 4 3 7 10 1 9
template <class Arg>
class FiboRand : public unary_function<Arg, Arg> {...
#include <iostream.h>
#include <algorithm>
#include <functional>
#include <list>
using namespace std;
// Data
int iarray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};list<int> aList(iarray, iarray + 10);
int main()
{int k = 0;
count_if(aList.begin(), aList.end(),
bind1st(greater<int>(), 8), k);cout << "Number elements < 8 == " << k << endl;
return 0;
}
bind1st(greater<int>(), 8)
8 > q
count_if(aList.begin(), aList.end(),
bind1st(greater<int>(), 8), k);
count_if(aList.begin(), aList.end(),
bind1st(greater<int>(), 8), k);
start = find_if(aList.begin(), aList.end(),
not1(bind1nd(greater<int>(), 6)));
[1] [2]
责任编辑 zz