TPU ChatChat on IRC
Internet Relay Chat?
Java Applet client
Who's on IRC?
C++ STL Matricies::complicated iterators
Posted by Zandalf [send private reply] at July 19, 2002, 01:45:51 AM Okay, i suppose this is really my first real programming question for the forum, but it's not an easy one. I've been studying the STL and generic programming recently, and I've come to love the idea of generalistic containers and algorithms. However, I've come upon a bit of a stumbling block in that I can't really figure out a way to approach doing two dimensional matricies using the STL.
Now, I know that the easy way to make matricies is to do something like:
and thus, elements of the matrix can be accessed easily by
Now, it's relatively simple to generate a forward iterator for a particular row, using
but I'm looking for a way to generate a forward iterator that traverses columns instead, something like:
So, I suppose this is generating a new type of iterator, one that requires the  operator to be defined for whatever type is contained in the container. So, it's not as general as much of the STL, but it should act (for all intensive purposes) as at least a bidirectional iterator (random access would probably be just as difficult, and allows for some nicer algorthms to be used)
Now, I know enough about iterators that I could probably hack out a soultion to this and play with it until it works, but I was wondering if anyone else had either a) previously done something like this, and wouldn't mind lending a few pointers, or b) has a better solution than using a
Posted by buzgub [send private reply] at July 19, 2002, 01:53:11 AM I'm guessing here, never having done serious stuff with C++ before, but I'm inclined to ask if you can make an object that inherits from the vector and fiddle with things that way.
I've no idea of the issues involved here, really, so feel free to ignore me.
Posted by CViper [send private reply] at July 19, 2002, 02:36:33 AM well. traversing a column (x) is simple:
for( y = 0; y < max; ++y ) matrix[y][x];
now simply write a wrapper class (the iterator) around that (which stores both x & y). Calling ++/-- will increment/decrement y, while x stays constant (add some bound checks).
note: you'll have to write some kind of wrapper around vector<vector<UDT>>, so that you can add the begin(x)
Posted by Psion [send private reply] at July 19, 2002, 08:08:10 AM I would say the "appropriate OO thing to do" would be to make a matrix template that inherits from vector, as buzgub suggested. I know we had such in a thing in the AP CS classes.
(And it's "all intents and purposes," not "all intensive purposes!" ;-)
Posted by Zandalf [send private reply] at July 19, 2002, 01:03:56 PM CViper - hehe, yes, it is about that simple, except that I want to write a generic algorithm that accepts a random access iterator, and for that to work I can't tell it which it should iterate (x or y), that has to be part of the internal coding of the iterator, but I think I'm figuring out how to do that.
buzgub, psion - it's interesting to try to figure out what the correct solution is here, because if I inherit from vector, i still only have a one dimensional array, and therefore I can't use the [y][x] notation. but if I have a vector<vector<UDT>> based on matrix<UDT>, then I can route the  operator to that data member's  operator, and everything works nicely. IOW, while inheritance is nice, I don't think that it serves the purposes of the program here, and instead the encapsulation that CViper suggests is a better alternative.
Please inform me of any errors in logic with that previous statement.
oh, and psion - my purposes are intesive!!! j/k, I had no idea that the phrase was "all intents and purposes", I'd just always thought of it the other way, and it still makes sense for the most part ^_^
Posted by Psion [send private reply] at July 19, 2002, 02:57:06 PM There's nothing to stop you from adding a method to get a column iterator to your matrix template.
Posted by taubz [send private reply] at July 20, 2002, 12:22:35 PM I would think writing a new Matrix class would work a lot better. A vector < vector > can have columns with different lengths, which doesn't make much sense for a matrix.
Posted by Zandalf [send private reply] at July 20, 2002, 06:19:46 PM hehe, better yet, I've just discovered that using vector<vector<UDT>> is illegal.
tab - yes, I thought about that, and was going to use the matrix class to keep them all the same size...
any ideas why I can't use vector<vector<>>?
Posted by Zandalf [send private reply] at July 20, 2002, 06:47:09 PM well, I managed to trick the system.
turns out that it will let me do this:
template <class E> typedef vector<E> subvector; vector<subvector> M;
so, for the moment, I think the problem is solved. but can anyone tell me what is wrong with vector<vector<E>>? or is that just a fault of VC++7?
Posted by taubz [send private reply] at July 20, 2002, 07:10:07 PM It's possible the >> is being read as the right-shift operator, which coincidentally I just read about recently (Java related tho). Try putting a space between the two >'s?
Posted by Zandalf [send private reply] at July 20, 2002, 07:15:31 PM hrmmm..
aarg! that's annoying.
Register as a new user