#include #include #include #include #include // Tips på hur man kan implementera Table_Policy::print och // Table_Policy::done vilket endast är relevant för VG-delen. // Om du inte tänker göra VG-delen kan du ignorera den utkommenterade // koden. // void print(std::ostream& os, int data) // { // os << std::setw(width) << data << ' '; // ++count; // if (count % columns == 0) // { // count = 0; // os << std::endl; // } // } // void done(std::ostream& os) // { // if (count != 0) // { // count = 0; // os << std::endl; // } // } class List_Policy { public: List_Policy(std::string const& bullet = "-") : bullet{bullet} { } void print(std::ostream& os, int data) { os << bullet << " " << data << std::endl; } void done(std::ostream&) { } private: std::string bullet; }; class Printer { public: Printer(std::ostream& os, List_Policy policy = {}) : os{os}, policy{policy} { } void print(std::vector::iterator begin, std::vector::iterator end) { for (std::vector::iterator current = begin; current != end; ++current) { policy.print(os, *current); } policy.done(os); } private: std::ostream& os; List_Policy policy; }; int main() { { std::vector v { 1, 23, 4, 567, 89 }; Printer p1 { std::cout }; p1.print(v.begin(), v.end()); // Testa p1.print(v) std::cout << std::endl; List_Policy l2 { "*" }; Printer p2 { std::cout, l2 }; p2.print(v.begin() + 1, v.begin() + 4); // Testa p2.print(v) } // Kom ihåg att testa för olika datatyper! // Testa även Table_Policy om du gör VG delen. }