I am working on a Matrix class that will eventually do a lot of stuff, but early on I have already hit a snag, how do I initialize (in a constructor) the 2D vector I have declared.

//matrix class
#ifndef MAT_CLASS
#define MAT_CLASS
//template <typename T>
using namespace std;
class Matrix {
      private:
              Matrix();
              unsigned rows;
              unsigned cols;
      protected:
              vector<vector<double>> mat;
      public:
             Matrix(unsigned Rows, unsigned Cols);
             Matrix operator+(Matrix mat);
             Matrix operator-(Matrix mat);
             Matrix operator*(double scalar);
             Matrix operator/(double scalar);
             Matrix operator*(Matrix mat);
             static double det(Matrix mat);
             Matrix multAddRow(unsigned row, double multiplier, unsigned rowToChange);
             Matrix multSubRow(unsigned row, double multiplier, unsigned rowToChange);
             Matrix divRow(double num);
             Matrix identity();
             Matrix gauss();
             Matrix reducedGauss();
             Matrix inverse();
};
Matrix::Matrix(){}
Matrix::Matrix(unsigned Rows, unsigned Cols){
                        rows=Rows;
                        cols=Cols;
                        //what do do here to make mat of size[Rows][Cols]
}


#endif

I am using Dev-C++ and the compiler that came with it.

Ok one big thing to note, before anything else--

vector<vector<double>>

-- is not a portable statement because >> may (depending on the compiler) be analyzed as the right bitshifting operator and throw an error. You'll want to space out those symbols.

vector<vector<double> >

Edit: Also I think you may need to push_back vectors into your main vector before you can access any elements in the matrix-vector.

commented: thanks i probably wouldn't have caught that +1

I am working on a Matrix class that will eventually do a lot of stuff, but early on I have already hit a snag, how do I initialize (in a constructor) the 2D vector I have declared.

//matrix class
#ifndef MAT_CLASS
#define MAT_CLASS
//template <typename T>
using namespace std;
class Matrix {
      private:
              Matrix();
              unsigned rows;
              unsigned cols;
      protected:
              vector<vector<double>> mat;
      public:
             Matrix(unsigned Rows, unsigned Cols);
             Matrix operator+(Matrix mat);
             Matrix operator-(Matrix mat);
             Matrix operator*(double scalar);
             Matrix operator/(double scalar);
             Matrix operator*(Matrix mat);
             static double det(Matrix mat);
             Matrix multAddRow(unsigned row, double multiplier, unsigned rowToChange);
             Matrix multSubRow(unsigned row, double multiplier, unsigned rowToChange);
             Matrix divRow(double num);
             Matrix identity();
             Matrix gauss();
             Matrix reducedGauss();
             Matrix inverse();
};
Matrix::Matrix(){}
Matrix::Matrix(unsigned Rows, unsigned Cols){
                        rows=Rows;
                        cols=Cols;
                        //what do do here to make mat of size[Rows][Cols]
}


#endif

I am using Dev-C++ and the compiler that came with it.

Not sure I completely understand what the problem is. You can use a 2-D vector right out of the gate and start putting information into it. You don't have to use the word "new" anywhere to set aside memory. Initialize it to whatever values you like using push_back. The following example creates a 2-D vector called mat and puts these values in it, then displays them:

[4.1 5.6]
[5.8 8.9]
#include <vector>
#include <iostream>
using namespace std;


int main ()
{
    vector <vector <double> > mat;
    
    vector <double> vector1;
    vector1.push_back(4.1);
    vector1.push_back(5.6);
    
    vector <double> vector2;
    vector2.push_back(5.8);
    vector2.push_back(8.9);
    
    mat.push_back(vector1);
    mat.push_back(vector2);
    
    for (int i = 0; i < mat.size(); i++)
    {
        for (int j = 0; j < mat[i].size(); j++)
        {
            cout << mat[i][j] << '\t';
        }
        cout << endl;
    }
    
    cin.get();
    return 0;
}
commented: I was thinking of that. You beat me to it though! +3
commented: thanks for the advice +1

thanks for the advice.

@VernonDozier i don't want to dynamically allocate the space with new.

@Alex thanks, i probably wouldn't have figured out the probable error message for a while.

so, i cannot just state that i want the vector(s) to have a size of say 10? i have to push_back numbers that may not be used?


@VernonDozier i don't want to dynamically allocate the space with new.

O.K. Good, because you don't have to.

so, i cannot just state that i want the vector(s) to have a size of say 10? i have to push_back numbers that may not be used?

You can specify an initial capacity of whatever you want with the reserve command. Make sure you don't confuse size and capacity. Size is the number of elements stored in the vector. Capacity is the number of elements that can be held before resizing.

The program below sets a capacity of 5 for vector2.

#include <vector>
#include <iostream>
using namespace std;


int main ()
{
    vector <vector <double> > mat;
    
    vector <double> vector1;
    vector1.push_back(4.1);
    vector1.push_back(5.6);
    
    vector <double> vector2;
    vector2.reserve(5);
    vector2.push_back(5.8);
    vector2.push_back(8.9);
    
    mat.push_back(vector1);
    mat.push_back(vector2);
    
    for (int i = 0; i < mat.size(); i++)
    {
        for (int j = 0; j < mat[i].size(); j++)
        {
            cout << mat[i][j] << '\t';
        }
        cout << endl;
    }
    
    
    cout << "vector2's capacity is " << vector2.capacity () << endl;
    cout << "vector2's size is " << vector2.size () << endl;
    
    cin.get();
    return 0;
}

Probably, the worst base for a matrix class is std::vector.

You lose all vector advantages (dynamic insert/delete ops) - no need in that features for matrix but gain inevitable std::vector slow access. All matrix operations are time-consuming, so the fastest access to rows, columns and selected elements is a key requirement for any successive matrix class.

In the best case you will get a plaster (vector;)) cast of a matrix.

thanks again for the info.

@VernonDozier that is what i needed thanks

@ArkM i'm not worried about speed at the moment, i don't want to use normal arrays because i would have to declare a set size, an i want the user to be able to do that. would there be a better container class to use or are you suggesting regular arrays

In actual fact no problems to resize and maintain dynamically allocated (via new op) arrays incapsulated in the matrix (or other) class. Don't forget: std::vector and std::string are based on "simple arrays" too. Of course, you may also use std::valarray (probably, the only STL container which was intended for math calculations). You may use std::vector - why not? I have seen a matrix class based on std::deque ;).

Apropos, make Google search: there are lots of freeware (and commercial) matrix classes in INET. Possibly these codes and articles studies help you to design your own (excellent) matrix class.

yes but i am not dynamically allocating the array i can't find much on valarray

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.