fork(2) download
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. class Vector {
  6. size_t size;
  7. double *elem;
  8. public:
  9. Vector(initializer_list<double> lst)
  10. :size{lst.size()},elem{new double[lst.size()]}
  11. {
  12. std::copy(lst.begin(),lst.end(),elem);
  13. }
  14. void test() { for (int i=0; i<size; i++) cout<< elem[i]<<" "; cout<<endl; }
  15. };
  16.  
  17. class Matrix{
  18. private:
  19. int row;
  20. int col;
  21. double **elem;
  22. public:
  23. //Default Constructor:
  24. Matrix(int row,int col) { cout << "Other !!"<<endl; }
  25. //Initialized list constructor:
  26. Matrix(initializer_list<initializer_list<double>> lst) : row{lst.size()},col{0} {
  27. for (auto &x: lst)
  28. if (x.size()>col)
  29. col = x.size();
  30. cout<<row<<"x"<<col<<endl;
  31. elem=new double*[row];
  32. auto it=lst.begin();
  33. for (int i=0; i<row; i++, it++) {
  34. elem[i]=new double[col];
  35. std::copy(it->begin(),it->end(),elem[i]);
  36. }
  37. }
  38. void test() {
  39. for (size_t i=0; i<row; i++) {
  40. for (size_t j=0; j<col; j++)
  41. cout<< elem[i][j]<<" ";
  42. cout<<endl;
  43. }
  44. }
  45. };
  46.  
  47. int main() {
  48. Vector v{1,2,3,4,5,6,7,8,9,10,11};
  49. v.test();
  50. Matrix m{ {1,2,3},{4,5,6},{7,8,9}};
  51. m.test();
  52. Matrix l{1,2};
  53. return 0;
  54. }
Success #stdin #stdout 0s 4240KB
stdin
Standard input is empty
stdout
1 2 3 4 5 6 7 8 9 10 11 
3x3
1 2 3 
4 5 6 
7 8 9 
Other !!