Your biggest problem is the way you're passing parameters to your functions. There are other fatal problems, but this one is going to cause your stack to each it's limit when you have nested for loops like you do.
Your constructor should be:
Matrix::Matrix(int sizeR, int sizeC, const std::vector<double>& input_data)
or
Matrix::Matrix(int sizeR, int sizeC, std::vector<double>* input_data)
In the second case, you will be dealing with a pointer, which is more complex, and care must be taken not to dereference a null pointer, so i recommend for ease of use, the first example.
Using a const std::vector<double>& will protect the vector from modification within the function it's passed to.
By using a reference of the vector, you won't be copying all of the data in each function call (in your posted code you're not even using the data in the first place anyway).
Next thing. This will copy the data when returned, which may or may not be what you intend, especially if these matrices ever become very large (think hundreds or thousands of row/cols which would take minutes to hours to complete the nested for loops).
std::vector<double> Matrix::getMatrixVals()
You might consider this instead, but it is ultimately up to what exactly you intend to do.
const std::vector<double>& Matrix::getMatrixVals() const
Now the next issue.
When you resize your data vector, you initialize it with garbage, and never actually assign any real data to it. Then you proceed to read the indeterminate data in the vector by copying it as parameters to subsequent function calls. This is
undefined behavior which should immediately fail an assignment.
instead of calling
data.resize(M*N);
just use:
1 2
|
data.reserve(input_data.size());
data.assign(input_data.begin(), input_data.end());
|
That will assign the data to the data vector and with the call to reserve() should do it as quickly as possible, though the reserve() is probably already done internally by the STL.
This line here is whats actually slowing down your for loops due to the reasons I explained above:
Matrix NewMatrix(endRow - startRow, endCol - startCol, newData);
By making a few changes inspired by my examples above, you should have less issues with the slow for loops. There are other things wrong with your code, but if i tried to cover them all in one post it would end up a huge mess as I'm not a very good teacher.