Sign in to follow this  
Si0n

Quick help

Recommended Posts

This matrix class is a little screwy, it is displaying weird numbers
#include <iostream>
#include <cstddef>
#include <vector>

template<typename T> class matrix
{
public:
    matrix(std::size_t r = 4, std::size_t c = 4, T val = T())
    	: rows(r), columns(c)
   	{	
   	    for (std::size_t k = 0; k < rows; k++) 
        {
   	        for (std::size_t j = 0; j < columns; j++) 
            {
   	            cols_vec.clear();
   	        	cols_vec.push_back(val);
   	        }   	
       	    rows_vec.push_back(cols_vec);
       	}
    }
            
    friend std::ostream& operator<<(std::ostream& out, const matrix& m)
    {
        for (std::size_t k = 0; k < m.rows; k++) 
        {
   	        for (std::size_t j = 0; j < m.columns; j++)
   	        	out << m.rows_vec[k][j] << ' ';
       	    out << '\n';
       	}
    }   	
    
    std::vector<T>& operator[](std::size_t i) { return rows_vec[i]; }
    const std::vector<T>& operator[](std::size_t i) const { return rows_vec[i]; }
    
private:
    std::size_t rows, columns;
    std::vector<T> cols_vec;
    std::vector<std::vector<T> > rows_vec;
}; 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this