View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

How to assign a multiple entries in array using [ ] and = operators

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

#1Misery  Members

Posted 05 July 2012 - 03:54 AM

Hi there,

I have a following problem: I would like to make an array template in C++ that would have similar operator like one in Scilab/Matlab:
Array[range] = scalar;

This expression would cause all elements of Array listed in range, take value of scalar. How do I do that?

class FloatArray
{
public:
float Elements[100];

//how do I define operator
???  operator [  ] (int *Range)
{ }

//and operator
????  operator = (???)
{ }
};

To obtain this?

Regards,
Misery

#2Hodgman  Moderators

Posted 05 July 2012 - 04:10 AM

Something along the lines of this code (untested), which returns a proxy object to perform the assignment should work:
struct Range

{

int begin, end;

};

class FloatRangeProxy

{

float* begin;

float* end;

public:

FloatRangeProxy( float* b, float* e ) : begin(b), end(e) {}

const float operator=( float f )

{

for( float* i=begin; i!=end; ++i )

*i = f;

return f;

}

};

class FloatArray

{

float Elements[100];

public:

FloatRangeProxy  operator [] (const Range& r)

{

return FloatRangeProxy( Elements + r.begin, Elements + r.end );

}

};

Edited by Hodgman, 05 July 2012 - 04:11 AM.

#3Matt-D  Members

Posted 08 July 2012 - 03:29 PM

As an inspiration, you can also look at open-source NT2 @ http://github.com/MetaScale/nt2/ which has a placeholder operator, nt2::_.
Example usage:
[source lang="cpp"]using nt2::_;nt2::table&lt;T&gt; a0, a1, b0, b1, a, b, c;a1 = nt2::sum(a0(_));b1(_,_,1) = 0.5*b0(_,_,1);c = nt2::horzcat(a(_), b(_));c = b(3, _);b(3, _) = b(2, _);b(2, _) = c;[/source]

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.