fork download
  1. #include<iostream>
  2. #include<vector>
  3.  
  4. template <typename T> struct Polynomial { std::vector<T> data; };
  5.  
  6. template <typename T>
  7. std::ostream& operator<<(std::ostream& o,const Polynomial<T>& pol) {
  8. const auto& data = pol.data;
  9. auto power = [](int n){
  10. return (n==0) ? "" : (n==1) ? "x" : "x^" + std::to_string(n);
  11. };
  12. auto sign = [&](int i){
  13. return (pol.data[i]<0 || i==pol.data.size()-1) ? "" : "+";
  14. };
  15. // RELY ON UNSIGNED OVERFLOW FOR THE LOOP CONDITION !!!
  16. for (size_t i = pol.data.size()-1; i < pol.data.size(); i-- ) {
  17. o << sign(i) << pol.data[i] << power(i);
  18. }
  19. return o;
  20. }
  21. //
  22. template <typename T> struct Matrix {
  23. using element_type = T;
  24. using row_type = std::vector<element_type> ;
  25. using data_type = std::vector<row_type> ;
  26. Matrix(int rows, int cols, T const & init) : data(data_type(rows,row_type(cols,init))) {}
  27. data_type data;
  28. };
  29.  
  30. template <typename T> std::ostream& operator<<(std::ostream& o,const std::vector<T>& v) {
  31. for (auto& x : v) o << x << "\t";
  32. return o;
  33. }
  34. template <typename T> std::ostream& operator<<(std::ostream& o,const Matrix<T>& mat) {
  35. for (const auto& row : mat.data) o << row << "\n";
  36. return o;
  37. }
  38.  
  39. int main() {
  40. using Matrix = Matrix<Polynomial<double>>;
  41. Polynomial<double> p1{{1,2}};
  42. Matrix mp(2,3,p1);
  43. std::cout << mp;
  44. return 0;
  45. }
Success #stdin #stdout 0s 4320KB
stdin
Standard input is empty
stdout
2x+1	2x+1	2x+1	
2x+1	2x+1	2x+1