Write a templated class named “EveryOther” that behaves much like std::vector (in the file named “everyother.h”). It has a member function called “push_back”, but only every other call to the member function actually does anything (actually pushed the element to the end of the structure). The class should have a default constructor and a constructor that takes an initializer list (be sure that you only add every other element). The other function that EveryOther needs to support is the operator<<, see the test cases for formating.
This class is templated so that instances like EveryOther<char> and EveryOther<vector<int>> are supported.
Input for testing the constructor:
#include<string>
EveryOther<char> eo_1;
EveryOther<std::string> eo_2 {“include”,”don’t”,”keep”,”leave”};
filename: everyother.h
#pragma once #include <iostream> #include <vector> #include <sstream> #include <initializer_list> using std::initializer_list; using std::vector; using std::ostream; using std::cout; template<typename T> class EveryOther; template<typename T> ostream & operator<<(ostream & o, const EveryOther<T>& e); template <class T> class EveryOther{ public: vector<T>vec_; EveryOther()=default; EveryOther(initializer_list<T> c): vec_(c){}; void push_back(T var){ vec_.push_back(var); } int size(){ vector<T>vec1; for(size_t i = 0; i < vec_.size(); i+=2){ vec1.push_back(vec_[i]); } return vec1.size(); } friend ostream & operator<< <T>(ostream &, const EveryOther&); }; template<typename T> ostream & operator<<(ostream & o,const EveryOther<T>& e){ o << "EveryOther("; for(size_t i = 0; i < e.vec_.size(); i+=2){ o << e.vec_[i] << ", "; } o << ')'; return o; }filename: main.cpp
#include <iostream> #include "everyother.h" int main() { EveryOther<char> eo_1; EveryOther<std::string> eo_2 {"include", "dont", "keep", "leave"}; std::cout << eo_2 << std::endl; }Output EveryOther(include, keep, )