Practice 1
namespace {
//show
template <typename T>
class show {
public:
void operator() (T n) {
cout << n << " ";
}
};
}
Practice 2
namespace{
template <typename T>
struct show : public unary_function<T, void> {
void operator() (T n) {
cout << n << " ";
}
};
}
Practice 3
namespace {
//out() algorithm
template <typename InputIterator, typename T>
void out(InputIterator first, InputIterator last) {
for_each(first, last, show<T>());
cout << endl;
}
}
Practice 4
#include <iostream>
#include <string>
#include <functional> // for funciton object
#include <algorithm> // for out() algorithm
#include <vector>
using namespace std;
// define show function object and out()
namespace {
//show
template <typename T>
struct show : public unary_function<T, void> {
void operator() (T n) {
cout << n << " ";
}
};
//out() algorithm
template <typename InputIterator, typename T>
void out(InputIterator first, InputIterator last) {
for_each(first, last, show<T>());
cout << endl;
}
}
int main() {
string strarray[] = {"a", "c", "l", "u", "w", "e", "c",
"z", "b", "l", "o", "r"};
int size = sizeof strarray / sizeof(string);
vector<string> v1(size);
v1.assign(strarray, strarray + size);
out<vector<string>::iterator, string>(v1.begin(), v1.end());
sort(v1.begin(), v1.end());
out<vector<string>::iterator, string>(v1.begin(), v1.end());
sort(v1.begin(), v1.end(), greater<string>());
out<vector<string>::iterator, string>(v1.begin(), v1.end());
return 0;
}