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:

Relevant Pages

  • Re: Puppy Mastiff wants to Nip at Faces
    ... in my first college textbook on structured programming. ... they did was loop through an array to show how you could easily ... design, PIC code, and real time programming. ...
  • Re: typedef, 2D arrays, design: question
    ... the author discusses "The Use of typedef". ... typedef scalar vector; ... array of the array vector? ... Abstraction is not all-or-nothing and it's not possible to say is some ...
  • Re: LISPPA
    ... > The abstract mathematical definition doesn't cope too well ... programming languages which do not allow to write bad programs, ... definition of array? ...
  • Re: How do i change numerics into binary numbers?
    ... my first time trying out on programming. ... how do I extract the output from the user to make it an array? ... Those are needed details. ... what you expect for input, (even if you know that it may be garbage, you have ...
  • Re: Bug/Gross InEfficiency in HeathFields fgetline program
    ... Maybe this is true in the sort of programming you do, ... you are storing a list of amounts of money as integers. ... int average ... the array, rounded towards zero. ...