C++ Template Question

Hans Meine hans_meine at gmx.net
Mon Aug 20 15:20:25 BST 2007


Am Montag, 20. August 2007 15:45:55 schrieb Mosfet:
> The only question I have about it is if it is possible to use this to
> create a multidimensional array and if so what is the proper way?

You mean sth. like this?

typedef QVarLengthArray<int, 50> Row;
QVarLengthArray<Row, 50> image(height);
for(uint y=0; y<height; ++y)
  image[y].resize(width);

Unfortunately, QVarLengthArray misses a lot of STL features, which makes the 
above code more complicated than necessary.

> I'm not a 
> template expert so don't know. I could of course just allocate one block
> and use offsets but I like using indexes if possible :)

..or you could use a "view"-like 2D adapter with a 1D QVarLengthArray.
(Our VIGRA library has such a thing: 
http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/doc/vigra/classvigra_1_1BasicImageView.html)

And there are other nice array classes, too:
http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/doc/vigra/classvigra_1_1TinyVector.html
http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/doc/vigra/classvigra_1_1ArrayVector.html

VIGRA is generic, i.e. can be used with Qt.  The references were meant as a 
general pointer, but you might indeed want to try it one day - no need to 
compile it if you are not interested in VIGRA's image loading/saving - the 
rest is just a bunch of header files.

Greetings,
  Hans




More information about the kde-core-devel mailing list