Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2015
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #include <vector>
  2. #include <numeric>
  3. #include <type_traits>
  4. #include <algorithm>
  5. #include <stdexcept>
  6. #include <iostream>
  7.  
  8. struct dataInType {
  9.     double value;       // what was voted on
  10.     int votes;       // how many times the value was voted on
  11.     using value_type = double;
  12.  
  13.     bool operator>(const dataInType &rhs) const {
  14.         return value > rhs.value;
  15.     }
  16.     bool operator<(const dataInType &rhs) const {
  17.         return value < rhs.value;
  18.     }
  19.     value_type operator+(const dataInType &rhs) const {
  20.         return value * votes + rhs.value* rhs.votes;
  21.     }
  22.     value_type operator+(value_type rhs) const {
  23.         return value * votes + rhs;
  24.     }
  25.     value_type operator-(const dataInType &rhs) const {
  26.         return value * votes - rhs.value * rhs.votes;
  27.     }
  28.     value_type operator-(value_type rhs) const {
  29.         return value * votes - rhs;
  30.     }
  31.     operator value_type() const {
  32.         return value * votes;
  33.     }
  34. };
  35.  
  36. std::ostream& operator<<(std::ostream &o, const dataInType &rhs) {
  37.     o << '{' << rhs.value << ',' << rhs.votes << '}';
  38.     return o;
  39. }
  40.  
  41. /**
  42.  * @param first
  43.  * @param last
  44.  * @return Unbiased sample standard deviation
  45.  *
  46.  * There is a bit of line noise here, but C++14 return-type deduction can fix it
  47.  */
  48. template<typename RandIt>
  49. auto std_dev(RandIt first, RandIt last) -> typename std::decay<decltype(*first)>::type::value_type {
  50.     using T = typename std::decay<decltype(*first)>::type;
  51.     using value_type = typename T::value_type;
  52.  
  53.     auto size = std::distance(first, last);
  54.     if (size < 2) {
  55.         if (size == 1) {
  56.             // This is an arbitrary decision
  57.             return value_type(*first);
  58.         }
  59.         throw std::out_of_range ("Empty range");
  60.     }
  61.     auto avg = std::accumulate(first, last, value_type { }) / size;
  62.  
  63.     auto sum = std::accumulate(first, last, value_type(),
  64.             [avg](value_type a, const T &x) {
  65.                 return a += (x - avg) * (x - avg);
  66.             }
  67.     );
  68.  
  69.     return std::sqrt(sum / (size - 1));
  70. }
  71.  
  72. int main() {
  73.     std::vector<dataInType> x = {
  74.             {12.1, 4}, {6.2, 5}, {0.34, 12}, {8.0, 1},
  75.             {6.7, 2}, {1.11, 17}, {0.65, 7}, {12.55, 8}
  76.     };
  77.  
  78.     auto max = *std::max_element(x.begin(), x.end());
  79.     auto stddev = std_dev(x.begin(), x.end());
  80.  
  81.     std::cout << "max = " << max << "\n"
  82.               << "stddev = " << stddev << "\n";
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement