#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iterator>
using namespace std;
template <typename T>
class MyStatistics {
private:
T m_Amount, m_Summation, m_Summation4Square;
public:
typedef MyStatistics<T> this_type;
explicit inline MyStatistics(void): m_Amount(T()), m_Summation(T()), m_Summation4Square(T()) {}
virtual inline ~MyStatistics(void) {}
public:
/*
* copy & back_inserter Requirement
*/
typedef const T &const_reference;
typedef T value_type;
inline this_type &push_back(const T &e) {
return AddElement(e);
}
public:
/*
* for_each Requirement
*/
inline MyStatistics(const this_type &d): m_Amount(d.m_Amount),
m_Summation(d.m_Summation), m_Summation4Square(d.m_Summation4Square) {}
inline this_type &operator=(const this_type &d) {
if(this != &d) {
m_Amount = d.m_Amount;
m_Summation = d.m_Summation;
m_Summation4Square = d.m_Summation4Square;
}
return *this;
}
inline this_type &operator()(const T &e) {
return AddElement(e);
}
public:
inline this_type &AddElement(const T &e = T()) {
m_Amount += 1;
m_Summation += e;
m_Summation4Square += e * e;
return *this;
}
inline this_type &Clear(void) {
m_Amount = T();
m_Summation = T();
m_Summation4Square = T();
return *this;
}
inline const T &GetNum(void) const {
return m_Amount;
}
inline const T &GetSum(void) const {
return m_Summation;
}
inline const double CalAverage(void) const {
return m_Summation / double(m_Amount);
}
inline const double CalStandardDeviation(void) const {
return sqrt(m_Summation4Square / double(m_Amount) - CalAverage() * CalAverage());
}
};
template <typename T>
inline ostream &operator <<(ostream &os, const MyStatistics<T> &rhs) {
return os << "Summation/Amount: " << rhs.GetSum() << "/ " << rhs.GetNum() << "\n"
<< "Average,Standard Deviation: " << rhs.CalAverage() << ", "
<< rhs.CalStandardDeviation() << "\n" ;
}
int main(int argc, char *argv[]) {
typedef vector<int> myIntArray_type;
MyStatistics<int> a;
int ia[] = {4, 5, 5, 6, 3, 3, 4, 5, 3, 4, 2, 3, 6, 4, 5, 5, 23, 5, 5, 3, 6, 7, 3, 7};
myIntArray_type myInstWidth_c(ia, ia + sizeof(ia) / sizeof(ia[0]));
copy(myInstWidth_c.begin(), myInstWidth_c.end(), back_inserter(a));
cout << a << endl;
a = for_each(myInstWidth_c.begin(), myInstWidth_c.end(), MyStatistics<int>());
cout << a << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <limits>
#include <cmath>
#include <ctime>
#include <iterator>
using namespace std;
class test {
public:
int m_x, m_y;
typedef test this_type;
inline test(const int &x = 0, const int &y = 0): m_x(x), m_y(y) {}
inline test(const this_type &d): m_x(d.m_x), m_y(d.m_y) {}
inline this_type &operator = (const this_type &d) {
m_x = d.m_x;
m_y = d.m_y;
return *this;
}
inline this_type &set(const int &x = 0, const int &y = 0) {
m_x = x;
m_y = y;
return *this;
}
};
inline ostream &operator<<(ostream &os, const test &d) {
ios_base::fmtflags ff = os.flags();
os << "(" << setw(3) << d.m_x << "," << setw(3) << d.m_y << ")";
os.flags(ff);
return os;
}
class testNulOp {
public:
enum Action {_random} action;
inline testNulOp(const Action &a = _random): action(a) {}
inline test operator()(void) const {
switch(action) {
case _random :
return test(rand() % 0xff, rand() % 0xff);
break;
default :
return test(0, 0);
break;
}
}
};
template <typename T>
class MyStatistics {
private:
T m_Minimum, m_Maximum, m_Amount, m_Summation, m_Summation4Square;
public:
typedef MyStatistics<T> this_type;
typedef numeric_limits<T> limits_type;
explicit inline MyStatistics(void)
: m_Minimum(limits_type::max()), m_Maximum(limits_type::min()), m_Amount(T()), m_Summation(T()), m_Summation4Square(T()) {}
inline MyStatistics(const this_type &d)
: m_Minimum(d.m_Minimum), m_Maximum(d.m_Maximum), m_Amount(d.m_Amount), m_Summation(d.m_Summation), m_Summation4Square(d.m_Summation4Square) {}
virtual inline ~MyStatistics(void) {}
inline this_type &operator=(const this_type &d) {
if(this != &d) {
m_Minimum = d.m_Minimum;
m_Maximum = d.m_Maximum;
m_Amount = d.m_Amount;
m_Summation = d.m_Summation;
m_Summation4Square = d.m_Summation4Square;
}
return *this;
}
inline this_type &operator()(const T &e) {
return AddElement(e);
}
public:
inline this_type &AddElement(const T &e = T()) {
m_Minimum = min(m_Minimum, e);
m_Maximum = max(m_Maximum, e);
m_Amount += 1;
m_Summation += e;
m_Summation4Square += e * e;
return *this;
}
inline this_type &Clear(void) {
m_Minimum = limits_type::max();
m_Maximum = limits_type::min();
m_Amount = T();
m_Summation = T();
m_Summation4Square = T();
return *this;
}
public:
inline const T &GetMin(void) const {
return m_Minimum;
}
inline const T &GetMax(void) const {
return m_Maximum;
}
inline const T &GetNum(void) const {
return m_Amount;
}
inline const T &GetSum(void) const {
return m_Summation;
}
inline const double CalAverage(void) const {
return m_Summation / double(m_Amount);
}
inline const double CalStandardDeviation(void) const {
return sqrt(m_Summation4Square / double(m_Amount) - CalAverage() * CalAverage()) ;
}
};
template <typename T>
inline ostream &operator <<(ostream &os, const MyStatistics<T> &d) {
ios_base::fmtflags ff = os.flags();
os << "limit min,max : " << d.GetMin() << ", " << d.GetMax() << "\n";
os << "Summation/Amount: " << d.GetSum() << "/ " << d.GetNum() << "\n";
os << "Average,Standard Deviation: " << d.CalAverage() << ", " << d.CalStandardDeviation() << "\n";
os.flags(ff);
return os;
}
template <typename T>
class MyStatisticsUniOp {
public:
enum Action {_x, _y} action;
private:
MyStatistics<T> m_data;
public:
typedef MyStatistics<T> basic_type;
typedef MyStatisticsUniOp<T> this_type;
explicit inline MyStatisticsUniOp(const Action &a = _x): action(a), m_data(basic_type()) {}
//inline MyStatisticsUniOp(const this_type& d): action(d.action), m_data(d.m_data) {}
//virtual inline ~MyStatisticsUniOp(void) {}
//inline this_type& operator=(const this_type& d) {
// if (this != &d) {
// action = d.action;
// m_data = d.m_data;
// }
// return *this;
//}
inline this_type &operator()(const test &e) {
switch(action) {
case _x :
m_data.AddElement(e.m_x);
break;
case _y :
m_data.AddElement(e.m_y);
break;
default :
break;
}
return *this;
}
inline operator basic_type(void) {
return m_data;
}
};
int main(int argc, char *argv[]) {
vector<test> myvector;
typedef vector<test>::const_iterator vtci;
srand((unsigned int)time(0) & 0xffULL);
generate_n(back_inserter(myvector), 10, testNulOp(testNulOp::_random));
copy(myvector.begin(), myvector.end(), ostream_iterator<test>(cout, "\n"));
cout << "--------------------------------------------------" << endl;
MyStatisticsUniOp<int> a;
MyStatistics<int> b;
b = for_each(myvector.begin(), myvector.end(), a);
cout << b << endl;
b.Clear();
MyStatisticsUniOp<int> c(MyStatisticsUniOp<int>::_y);
b = for_each(myvector.begin(), myvector.end(), c);
cout << b << endl;
b.Clear();
system("PAUSE");
return EXIT_SUCCESS;
}
沒有留言:
張貼留言