Array using STL

H

hlg

I have a question, which must surely have occurred to many programmers
since STL first appeared, and yet I have found no reference to it
anywhere, suggesting the problem is insoluble. Nevertheless, here it is:

I wish to create an two-dimensional array of objects. On the one hand,
it is useful to use STL containers for the rows and columns of the
array. On the other, it would be nice to address the elements by the
array element operator [] i.e. myObj obj = myObjArray[x,y];

So far I have tried implementing this as:

//
class myObj {...};

//
typedef std::vector<myObj> myObjColumn;
typedef std::vector<myObjColumn> myObjRow;

....

myObjRow myObjArray;

//
// retrieve an instance of myObj from array
//
myObj& GetMyObj (UINT x, UINT y)
{
// check for invalid indexes
...
myObjColumn col = myObjArray[x];
myObj obj = col[y];
return obj;
}

So far, so good. Unfortunately, the ratting fratting MS Visual C++
compiler will not allow the next logical step:

#define myObjArray[x,y] GetMyObj(x,y)

.... as I get the error message

"error C2008: '[' : unexpected in macro definition"


So, has anyone a solution ? (Or am I on the wrong track entirely ?)

hlg
 
R

Robbie Hatley

hlg said:
I have a question, which must surely have occurred to many programmers
since STL first appeared, and yet I have found no reference to it
anywhere, suggesting the problem is insoluble.

Nah. Just means you haven't seen a reference to it.
I wish to create an two-dimensional array of objects. On the one hand,
it is useful to use STL containers for the rows and columns of the
array. On the other, it would be nice to address the elements by the
array element operator [] i.e. myObj obj = myObjArray[x,y];

That is non-standard nomenclature. Array elements in most
languages are Blat[7][5], not Blat[5,7]. I'm not sure your
desired syntax CAN be implimented in C++. You could easily
do Blat(5,7) using an application operator; but Blat[5,7] is
more problematic.
So far I have tried implementing this as:

//
class myObj {...};

//
typedef std::vector<myObj> myObjColumn;
typedef std::vector<myObjColumn> myObjRow;

Ewww. No. A row should be a vector of the objects you want
to store. The 2-D array itself is a (vertical) array of rows.
A "column" would be a slice, better done with valarray.
If you don't needs slices, then you can just use a vector of
vectors:

std::vector<std::vector<MyType> > MyArray;

Or better yet, use a template. Something like the following
(untested, off the top of my head) should work:

template<typename T>
class MyArrayType
{
public:
// Construct array:
MyArrayType(int width, int height)
{
repre_ = new std::vector<std::vector<T>* > (height);
for (int i = 0; i < height; ++i)
{
repre_ = new std::vector<T> (width);
}
}

// Destruct array:
~MyArrayType(int width, int height)
{
for (int i = height-1; i >= height; --i)
{
delete repre_;
}
delete repre_;
}

// Get reference to element of array:
T & operator()(int x, int y)
{
return representation_[y][x];
}
private:
// Underlying representation:
std::vector<std::vector<T>* >* repre_;
};

(The above is not guaranteed to be error free. You'll
have to debug and refine it yourself. But it should
give you an idea of a way to proceed.)
myObjRow myObjArray;

//
// retrieve an instance of myObj from array
//
myObj& GetMyObj (UINT x, UINT y)
{
// check for invalid indexes
...
myObjColumn col = myObjArray[x];
myObj obj = col[y];
return obj;
}

So far, so good. Unfortunately, the ratting fratting MS Visual C++
compiler will not allow the next logical step:

#define myObjArray[x,y] GetMyObj(x,y)

... as I get the error message

"error C2008: '[' : unexpected in macro definition"

That's not a Microsoft issue. That's just the way the C
preprocessor is. Functionlike macros use parentheses, not
brackets.
So, has anyone a solution ? (Or am I on the wrong track entirely ?)

If you can use parentheses, Blat(5, 7), then yes. (see above.)

But if you insist on brackets, then I don't see a way to do that.
Maybe someone else here can see a way.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

I have a question, which must surely have occurred to many programmers
since STL first appeared, and yet I have found no reference to it
anywhere, suggesting the problem is insoluble.

Read this and the following FAQs:
http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.10
Nevertheless, here it is:

I wish to create an two-dimensional array of objects. On the one hand,
it is useful to use STL containers for the rows and columns of the
array. On the other, it would be nice to address the elements by the
array element operator [] i.e. myObj obj = myObjArray[x,y];

Sorry, but no can do, operator[] only takes one parameter, use
operator() or [][]-syntax.
 
J

James Kanze

Read this and the following
FAQs:http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-1...
Nevertheless, here it is:
I wish to create an two-dimensional array of objects. On the one hand,
it is useful to use STL containers for the rows and columns of the
array. On the other, it would be nice to address the elements by the
array element operator [] i.e. myObj obj = myObjArray[x,y];
Sorry, but no can do, operator[] only takes one parameter, use
operator() or [][]-syntax.

Well, his expression only gives it one argument. That's really
where the problem lies, and why you can't overload the way he
would like: myObjArray[x,y] has only a single index, the
expression "x,y", which evaluates x, throws out the result, then
evaluates y, and uses it. Because it is already so defined, you
can't redefine it without breaking code. One might argue that
such code deserves to be broken, but the committee doesn't like
to break code, even if it is bad code.
 
T

terminator

I have a question, which must surely have occurred to many programmers
since STL first appeared, and yet I have found no reference to it
anywhere, suggesting the problem is insoluble. Nevertheless, here it is:

I wish to create an two-dimensional array of objects. On the one hand,
it is useful to use STL containers for the rows and columns of the
array. On the other, it would be nice to address the elements by the
array element operator [] i.e. myObj obj = myObjArray[x,y];

So far I have tried implementing this as:

//
class myObj {...};

//
typedef std::vector<myObj> myObjColumn;
typedef std::vector<myObjColumn> myObjRow;

...

myObjRow myObjArray;

//
// retrieve an instance of myObj from array
//
myObj& GetMyObj (UINT x, UINT y)
{
// check for invalid indexes
...
myObjColumn col = myObjArray[x];
myObj obj = col[y];
return obj;

}

So far, so good. Unfortunately, the ratting fratting MS Visual C++
compiler will not allow the next logical step:

#define myObjArray[x,y] GetMyObj(x,y)

... as I get the error message

"error C2008: '[' : unexpected in macro definition"

So, has anyone a solution ? (Or am I on the wrong track entirely ?)

hlg

typedef std::vector<myObj> myObjColumn;
typedef std::vector<myObjColumn> myObjRow;

....

myObjRow myObjArray;
myObjArray [x] [y] = myObjArray [x+1] [y]; /*double indexing needs two
braces not one enclosing a comma*/


regards,
FM.
 
H

hlg

terminator said:
I have a question, which must surely have occurred to many programmers
since STL first appeared, and yet I have found no reference to it
anywhere, suggesting the problem is insoluble. Nevertheless, here it is:

I wish to create an two-dimensional array of objects. On the one hand,
it is useful to use STL containers for the rows and columns of the
array. On the other, it would be nice to address the elements by the
array element operator [] i.e. myObj obj = myObjArray[x,y];

So far I have tried implementing this as:

//
class myObj {...};

//
typedef std::vector<myObj> myObjColumn;
typedef std::vector<myObjColumn> myObjRow;

...

myObjRow myObjArray;

//
// retrieve an instance of myObj from array
//
myObj& GetMyObj (UINT x, UINT y)
{
// check for invalid indexes
...
myObjColumn col = myObjArray[x];
myObj obj = col[y];
return obj;

}

So far, so good. Unfortunately, the ratting fratting MS Visual C++
compiler will not allow the next logical step:

#define myObjArray[x,y] GetMyObj(x,y)

... as I get the error message

"error C2008: '[' : unexpected in macro definition"

So, has anyone a solution ? (Or am I on the wrong track entirely ?)

hlg

typedef std::vector<myObj> myObjColumn;
typedef std::vector<myObjColumn> myObjRow;

...

myObjRow myObjArray;
myObjArray [x] [y] = myObjArray [x+1] [y]; /*double indexing needs two
braces not one enclosing a comma*/


regards,
FM.

Thanks all. To summarise:

The syntax I was looking for was [][] ... silly me ! (I'm a bit rusty)
.... and the double subscript operator can't be overloaded. Damn ! Never
mind. Press on.

hlg
 
O

Old Wolf

#define myObjArray[x,y] GetMyObj(x,y)
... as I get the error message
"error C2008: '[' : unexpected in macro definition"

I think this is a compiler bug; the above should
define an object-like macro, replacing the string
'myObjArray' with '[x,y] GetMyObj(x,y)'. The
specification of object-like macros doesn't seem
to require whitespace between the macro name and the
replacement text.
The syntax I was looking for was [][] ... silly me ! (I'm a
bit rusty)and the double subscript operator can't be overloaded.

However you can overload [] to return a custom class
that itself overloads [], getting the same effect.
 
T

terminator

#define myObjArray[x,y] GetMyObj(x,y)
... as I get the error message
"error C2008: '[' : unexpected in macro definition"

I think this is a compiler bug; the above should
define an object-like macro, replacing the string
'myObjArray' with '[x,y] GetMyObj(x,y)'. The
specification of object-like macros doesn't seem
to require whitespace between the macro name and the
replacement text.
The syntax I was looking for was [][] ... silly me ! (I'm a
bit rusty)and the double subscript operator can't be overloaded.

However you can overload [] to return a custom class
that itself overloads [], getting the same effect.

double parametrized operator() could be overloaded as well but someone
may prefer an indexer class as you pointed out:

struct array{
class value_type;
...
...
friend class indexer;
class indexer{
value_type & operator[](unsigned yyy);//[x] [yyy]
...
};

indexer & operator[](unsigned xxx);//[xxx] [y]

value_type & operator()(unsigned,unsigned);//(x,y)==[x][y]

};

regards,
FM.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top