Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <numeric>
- using namespace std;
- template <typename T>
- class Average {
- public:
- void add(const T &value) {
- vec.push_back(value);
- }
- double average() {
- return accumulate(vec.begin(), vec.end(), 0.0) / vec.size();
- }
- void reset(){
- vec.clear();
- }
- void print() const{
- bool first = true;
- cout << "[";
- for (auto const &item : vec) {
- if (first) {
- cout << item;
- first = false;
- }
- else {
- cout << ", " << item;
- }
- }
- cout << "]" << endl;
- }
- private:
- vector<T> vec;
- };
- int main()
- {
- Average<float> a1;
- a1.add(1.5);
- a1.add(6);
- a1.add(2);
- a1.add(9);
- a1.print();
- cout << "Average: " << a1.average() << endl;
- a1.reset();
- a1.print();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement