简体   繁体   中英

constructor function for a 3x3 matrix (C++)

I need to write constructors for my 3x3 matrix class. I'm not sure whether I'm doing it efficiently and I do not know how to use initializer_list.. I would like a constructor that creates by default the identity matrix, and receives parameteres to set a diagonal matrix. Then I'd also like a constructor that takes a list of cofficients (with initialization_list) and puts them in the matrix. This is what I have:

#include <iostream>
#include <array>
#include <cmath>
#include <initializer_list>
#include "Vecteur.h"
using namespace std;

   class Matrice33 {


private :

   array<array<double,3>,3> matrice;

   public :

       Matrice33(double a = 1, double b=1, double c=1)

        { matrice[0][0] = a; matrice[1][1]= b; matrice [2][2]= c;
          matrice[0][1] = 0; matrice[0][2] = 0; 
          matrice[1][0] = 0; matrice[1][2] = 0;
          matrice[2][0] = 0; matrice[2][1] = 0;} \\ IS THIS THE ONLY WAY TO SET THE MATRIX OR IS 
                                                  \\THERE A QUICKER ONE?



      Matrice33(initilizer_liste<double> cosnt& coeff)

        { for( auto c : coeff) \\ I DON'T KNOW HOW TO WRITE THIS CONSTRUCTOR...



    void affiche() const { for(auto m : matrice){
                              for(auto n : m) {
                                  cout<<n<<" ";}
                                 cout<<endl;}}

      }; 

   int main(){

    Matrice33 mat(1.1, 1.2, 1.3,
                  2.1, 2.2, 2.3,
                  3.1, 3.2, 3.3); \\I'D LIKE THIS TO BE TAKEN AS THE LIST BY MY CONSTRUCTOR, FOR 
                                   \\EXAMPLE

      Matrice33 m(2.0,3.0,1.0);
      m.affiche(); \\ WHEN I EXECUTE I DON'T GET ANY OUTPUT.. 
        return 0;
           }

This is changed version of your code that is just working.

#include <iostream>
#include <array>
#include <cmath>
#include <initializer_list>
#include <vector>

using namespace std;

class Matrice33
{
public:

   Matrice33(double a = 1, double b = 1, double c = 1)
      : matrice{{ {{a, 0, 0}}, {{0, b, 0}}, {{0, 0, c}} }}
   {
   }

   Matrice33(const std::initializer_list<std::initializer_list<double>> & coeff)
   {
      int counter = 0;
      for (auto row : coeff)
      {
         copy(row.begin(), row.end(), matrice[counter++].begin());
      }
   }

   void affiche()
   {
      for (int i = 0; i < 3; i++)
      {
         for (int j = 0; j < 3; j++)
         {
            cout << matrice[i][j] << " ";
         }
         cout << endl;;
      }
   }

private:
   array<array<double, 3>, 3> matrice;
};

int main() 
{

   Matrice33 mat{ {1.1, 1.2, 1.3},{2.1, 2.2, 2.3},{3.1, 3.2, 3.3} };
   mat.affiche(); 

   Matrice33 m(2.0, 3.0, 1.0);
   m.affiche(); 

   return 0;
}

You can use perfect forwarding and template constructor here.

template<typename... Args>
Matrice33(Args&&... args) : matrice(std::forward<Args>(args)...) {}

then initialization will be same as intitialization of std::array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM