delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/21/14:32:12

From: Charles Krug <charles AT pentek DOT com>
Newsgroups: comp.os.msdos.djgpp,comp.lang.c++
Subject: Re: 2d Matrix class with conventional c/c++ element access semantics
Date: Tue, 20 Jan 1998 10:02:08 -0500
Lines: 53
Message-ID: <34C4BC70.8E63DBE0@pentek.com>
References: <34C36C45 DOT 24966424 AT pentek DOT com> <199801200038 DOT RAA49356 AT huey DOT cadvision DOT com>
NNTP-Posting-Host: mail.pentek.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Thank you, groups, for your suggestions.  My problem was confusing two different
methods of construction.  I did not consider using a single vector with an
accessor method based on (m, n).  My goal was a drop-in replacement for code
that operates on conventional C 2d arrays.  Hence, the accessor method using
[m][n] was essential.  Although I wrote a multiplication method to test my
accessor methods, I do not expect to use this class for matrix math.  I am using
the code to implement an adjacency matrix, which I am using to solve a digital
logic problem.  Since my adjacency matrix code operates on conventional C
arrays, preserving accessor semantics was extremely important to me.  Your
milage may vary.  Here is the class interface:

#include <vector>

template <class T>
class matrix {
private:
    // Things that I want to avoid, generally speaking

public:
    matrix() : rows() {}
    matrix(const unsigned int nor,
           const unsigned int noc,
           const T val = T())
           :rows(nor, vector<T>(noc, val)) {}

    matrix(const matrix<T> & v)
           :rows(v.rows) {}

    matrix<T> & operator = (const matrix<T> & val)
    {
        if (&val == this) return *this;
        rows = val.rows;
        return *this;
    }

    ~matrix() {}
    vector<T> & operator [ ] (unsigned int i)
        { return rows[i];}

    unsigned int numberOfRows() const
    { return rows.size(); }

    unsigned int numberOfColumns () const
    { return rows[0].size(); }

protected:
    vector<vector<T> > rows;
};

--
Charles Krug, Jr.


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019