Re: vector vs. valarray

From: Francis Glassborow (
Date: 06/18/04

Date: Fri, 18 Jun 2004 10:09:52 +0100

In article <3tvAc.816$>, ctick
<> writes
>1. How ro decide which one to use, vector<int> or valarray<int>?

Unless you have very special numerical needs avoid valarray. It was
designed for the needs of numerically intense programming and has a
number of limitations in order to allow better optimisation,
particularly on hardware that has an array processor.

>2. How to use them for 3D array?

If you want to go to 3D, for get valarray -- it is designed for 1D
arrays and for matrices. Using it for 3D with out contravening the
limitation on nesting depth for template arguments is hard unless you
are both a proficient programmer and a computational expert.

For the vectors code and coding becomes much easier if you use some

typedef int elem_t;
typedef std::vector<element_t> row_t;
typedef std::vector<row_t> plane_t;
typedef std::vector<plane_t> space_t;

int main(){
   int x, y, z;
// code initialising x, y and z
   space_t solid(x, plane_t(y, row_t(z elem_t())));
// that is the hard part done
// from now on just treat solid as a 3D array

Note that ultimately rows can be of different lengths, planes do not
have to be rectangular and that solids do not have to be 'cuboids'
however once you go down that route the object is no longer symmetrical
in its three dimensions.

Note the typedefs are only to assist in coding (for which typedef
deserves to be used more often -- experienced programmers tend to use
typedef and books for novices tend to ignore them. The latter is, IMO,
making life more difficult than it needs to be for the newcomer).

Francis Glassborow      ACCU
Author of 'You Can Do It!' see
For project ideas and contributions: