Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to define a class which looks like an array?

52 views
Skip to first unread message

fl

unread,
May 25, 2015, 8:10:53 PM5/25/15
to
Hi,

I get a piece of code which is useful for my project. The code is incomplete.
Thus, I want to modify it to work for me. The problem is from a matrix usage.
It has the following snippet:





matrix state(6,1);

/*initializes the state*/
state[0][0]=0.1;





The matrix is known as two dimension, which looks just like a two dimension
array. Is it a pseudo code on the state variable? If it is the real code,
I don't know how to define the class to make it like a two dimensional array.

Could you help me on the problem?

Thanks,

Ian Collins

unread,
May 25, 2015, 8:18:44 PM5/25/15
to
constexpr size_t X = some size;
constexpr size_t Y = some size;

using Matrix = std::array<std::array<double,Y>,X>;

Matrix matrix;

--
Ian Collins

fl

unread,
May 25, 2015, 10:17:53 PM5/25/15
to
Thank Ian. Your reply is really helpful to me. I am still new to C++.
Could you make it a real specific example of your pseudo code? I have tried
some time, but it does not work yet. For example, what constexpr would be?
size_t is a type?




fl

unread,
May 25, 2015, 10:24:18 PM5/25/15
to
On Monday, May 25, 2015 at 5:18:44 PM UTC-7, Ian Collins wrote:
I have tried this:

Matrix = std::array<std::array<double,6>,6>;


missing type specifier - int assumed.
'array' : is not a member of 'std'
'array' : undeclared identifier
type 'double' unexpected

The above message is from MSVC compiler.

Could you help me through the build?

Thanks

Ian Collins

unread,
May 26, 2015, 12:16:57 AM5/26/15
to
fl wrote:
> On Monday, May 25, 2015 at 5:18:44 PM UTC-7, Ian Collins wrote:
>> fl wrote:
>>> Hi,
>>>
>>> I get a piece of code which is useful for my project. The code is incomplete.
>>> Thus, I want to modify it to work for me. The problem is from a matrix usage.
>>> It has the following snippet:
>>>
>>> matrix state(6,1);
>>>
>>> /*initializes the state*/
>>> state[0][0]=0.1;
>>>
>>>
>>> The matrix is known as two dimension, which looks just like a two dimension
>>> array. Is it a pseudo code on the state variable? If it is the real code,
>>> I don't know how to define the class to make it like a two dimensional array.
>>
>> constexpr size_t X = some size;
>> constexpr size_t Y = some size;
>>
>> using Matrix = std::array<std::array<double,Y>,X>;
>>
>> Matrix matrix;
>
> Thank Ian. Your reply is really helpful to me. I am still new to C++.
> Could you make it a real specific example of your pseudo code? I have tried
> some time, but it does not work yet. For example, what constexpr would be?
> size_t is a type?

It wasn't pseudo code!

Your C++ book should explain constexpr and size_t.

Let's try again with a complete example:

#include <array>

constexpr size_t X = 10;
constexpr size_t Y = 10;

using Matrix = std::array<std::array<double,Y>,X>;

int main()
{
Matrix state;
state[0][0] = 0.1;
}

--
Ian Collins

Ian Collins

unread,
May 26, 2015, 12:18:08 AM5/26/15
to
fl wrote:
> On Monday, May 25, 2015 at 5:18:44 PM UTC-7, Ian Collins wrote:
>> fl wrote:
>>> Hi,
>>>
>>> I get a piece of code which is useful for my project. The code is incomplete.
>>> Thus, I want to modify it to work for me. The problem is from a matrix usage.
>>> It has the following snippet:
>>>
>>> matrix state(6,1);
>>>
>>> /*initializes the state*/
>>> state[0][0]=0.1;
>>>
>>>
>>> The matrix is known as two dimension, which looks just like a two dimension
>>> array. Is it a pseudo code on the state variable? If it is the real code,
>>> I don't know how to define the class to make it like a two dimensional array.
>>
>> constexpr size_t X = some size;
>> constexpr size_t Y = some size;
>>
>> using Matrix = std::array<std::array<double,Y>,X>;
>>
>> Matrix matrix;
>>
>> --
>> Ian Collins
>
> I have tried this:
>
> Matrix = std::array<std::array<double,6>,6>;

Why did you omit the "using"? It wasn't prose, it's a C++ keyword!

--
Ian Collins

fl

unread,
May 26, 2015, 2:29:05 AM5/26/15
to
Excuse me. I did use using at first, but it still can't pass build with
MSVC. Now, it is found that MSVC does not support c++11.



Whether below line is equivalent to typedef in MSVC 2010?

using Matrix = std::array<std::array<double,Y>,X>;



Now I find that a two-dimensional array is similar to a Matrix in the
project. My new question is whether a two-dimensional array can be
redefined as a Matrix in order to support matrix multiplication in the
following (A, P, Q are nXn Matrix, ' is transpose operation)?


p = A * P * A' + Q;




Thanks again.

Paavo Helde

unread,
May 26, 2015, 3:20:31 AM5/26/15
to
fl <rxj...@gmail.com> wrote in
news:3c5e4b08-055c-4033...@googlegroups.com:
>
> Excuse me. I did use using at first, but it still can't pass build
> with MSVC. Now, it is found that MSVC does not support c++11.
> Whether below line is equivalent to typedef in MSVC 2010?


So why don't you upgrade to a newer version of MSVC, there has been at
least 3 releases after MVSC 2010 and the C++11 standard is 4 years old.
Nobody in public forums is interested in providing outdated and inferior
solutions.

Cheers
Paavo

Chris Vine

unread,
May 26, 2015, 5:50:30 AM5/26/15
to
With C++98/03, you can define an alias for a matrix type using the
built-in array syntax:

const size_t X = 6;
const size_t Y = 4;

typedef double Matrix[Y][X]; // Matrix is now a type alias for a
// statically sized 4×6 matrix of doubles

Using this approach you will need to write vector and matrix
multiplication functions yourself, which take and return such matrices.
They can accordingly be tedious to use. A more advanced approach is to
write a Matrix class, with multiplication and addition as overloaded
operators for the class.

In such a class, for a dynamically sized matrix you probably would not
use a multi-dimensional matrix internally, but instead use pointer
arithmetic on a single dimensional array to arrive at the correct cell.
This is because matrix[2][3] is equivalent to arr[ncols * 2 + 3],
where ncols is the number of columns (the X value above).

Read also:
https://isocpp.org/wiki/faq/freestore-mgmt#multidim-arrays
https://isocpp.org/wiki/faq/freestore-mgmt#multidim-arrays2
and the immediately following FAQs. Matrices are tricky in C++, with a
number of different approaches which involve different trade-offs for
static and dynamic sizing and exception safety, which is why they are
often part of a homework assignment :)

Chris

Öö Tiib

unread,
May 26, 2015, 6:46:48 AM5/26/15
to
On Tuesday, 26 May 2015 09:29:05 UTC+3, fl wrote:
> On Monday, May 25, 2015 at 9:18:08 PM UTC-7, Ian Collins wrote:
> > fl wrote:
> > > On Monday, May 25, 2015 at 5:18:44 PM UTC-7, Ian Collins wrote:
> > >> fl wrote:
> > >>> I get a piece of code which is useful for my project. The code
> > >>> is incomplete.
> > >>> Thus, I want to modify it to work for me. The problem is from
> > >>> a matrix usage.
> > >>> It has the following snippet:
> > >>>
> > >>> matrix state(6,1);
> > >>>
> > >>> /*initializes the state*/
> > >>> state[0][0]=0.1;
> > >>>
> > >>>
> > >>> The matrix is known as two dimension, which looks just like a
> > >>> two dimension array. Is it a pseudo code on the state variable?
> > >>> If it is the real code, I don't know how to define the class to
> > >>> make it like a two dimensional array.
> > >>
> > >> constexpr size_t X = some size;
> > >> constexpr size_t Y = some size;
> > >>
> > >> using Matrix = std::array<std::array<double,Y>,X>;
> > >>
> > >> Matrix matrix;
> > >
> > > I have tried this:
> > >
> > > Matrix = std::array<std::array<double,6>,6>;
> >
> > Why did you omit the "using"? It wasn't prose, it's a C++ keyword!
>
> Excuse me. I did use using at first, but it still can't pass build
> with MSVC. Now, it is found that MSVC does not support c++11.

'std::array' is a simple template mostly copied from 'boost::array' and
it was certainly present in C++ library of MSVC 2010.

> Whether below line is equivalent to typedef in MSVC 2010?
>
> using Matrix = std::array<std::array<double,Y>,X>;

Yes. It is just easier to read than:

typedef std::array<std::array<double,Y>,X> Matrix;

Why you start a new project with 3 generations old compiler?

> Now I find that a two-dimensional array is similar to a Matrix in the
> project.

What you mean by a two-dimensional array? You mean raw array? It is
rather tricky to work with raw arrays in C++ since those decay to
pointer of first element on slightest provocation (that means almost
always when you try to use one for anything). It is therefore
advisable to encapsulate raw array to a class to make it bit more
manageable (and that is what std::array does).

> My new question is whether a two-dimensional array can be
> redefined as a Matrix in order to support matrix multiplication in the
> following (A, P, Q are nXn Matrix, ' is transpose operation)?
>
>
> p = A * P * A' + Q;

No. There are no such thing as post-fix operator ' in C++ and there
are no such thing as matrix operations in C++ and also you can't make
operators (or functions) that return raw arrays in C++. Raw arrays
can't be passed or returned by value (as legacy from C). You should
read a good beginner level book about C++; you won't get anywhere by
asking these novice level questions in forums.

When we need to do linear algebra with matrices in C++ then we usually
use something outside of standard C++ for example some linear algebra
library (Armadillo, Eigen or Boost.UBLAS). These already contain
classes for representing matrices so you should not invent your own
types of matrices but simply use theirs.

Nobody

unread,
May 26, 2015, 5:54:29 PM5/26/15
to
On Mon, 25 May 2015 23:28:45 -0700, fl wrote:

> Excuse me. I did use using at first, but it still can't pass build with
> MSVC. Now, it is found that MSVC does not support c++11.

This version works in VS 2010:

#include <array>

static const size_t X = 5;
static const size_t Y = 6;

typedef std::tr1::array<std::tr1::array<double,X>,Y> Matrix;

0 new messages