C++
C#
VB
JScript
All

# Arrays

Copyright (C) 2005 IENT-RWTH Aachen

Vectors and matrices (Arrays) are the most common containers.

If you only plan to use vectors, just include the vector.h header file in your programme. If you also plan to use matrices, just include the matrix.h header file, this will automaticly include the vector.h header file as well.

Every array type is internaly represented with a generator that describes all its behaviours. It is a bit fastidious to declare the right generator, that is why interfaces were programmed to easily create the right array type. These interfaces can be considered as “functions” that return a type. The result type is contained in the self keyword.

The far most used arrays are dense, the corresponding interfaces are DenseVector and DenseMatrix. In this case, the interfaces have a template parameter for the element type the array will be filled with.

The following example initialises various dense vectors and applies basic calculations on them.

    #include "array/vector.h"

    int main()
    {
      typedef DenseVector<int>::self intVector;

      intVector X(4, "1 2 3 4"); // Vector of 4 integers initialised with 1,2,3,4
      cout << X << endl; // displays the content

      DenseVector<float>::self Y(X.size()); // Vector of 4 floats not initialised
      for (int i=0; i<Y.size(); ++i) Y[i]=i; // Overwrites every element of Y
      cout << Y << endl; //displays the content

      typedef DenseVector<intVector>::self MyVector; // Vector of vectors
      MyVector Z(3); // contends 3 vectors
      Z[0]=X;
      Z[1]=2*X;
      Z[2]=intVector(5,0); // Vector of 5 integers all initialised with 0
    }

The following example initialises various dense matrices and applies basic calculations on them.

    #include "array/matrix.h"

    int main()
    {
      typedef DenseVector<int>::self intVector;
      typedef DenseMatrix<int>::self intMatrix;

      intMatrix X(2,2, "1 2 3 4"); // Matrix of 2x2 integers initialised with 1,2,3,4
      cout << X << endl; // displays the content

      DenseMatrix<float>::self Y(X.size()); // Matrix of 2x2 floats not initialized
      for (int i=0; i<Y.nrows(); ++i)
        for (int j=0; j<Y.ncols(); ++j)
          Y(i,j)=i+j; // Overwrites every element of Y
      cout << Y << endl; //displays the content

      typedef DenseMatrix<intVector>::self MyMatrix; // Matrix of vectors
      MyMatrix Z(2,2); // contends 2x2 vectors
      Z(0,0)=intVector(2,"1 2");
      Z(0,1)=intVector(3,"1 2 3");
      Z(1,0)=intVector(5,0);
      Z(1,1)=3*Z(0,0);
      cout << Z << endl;
    }

See Also

Tutorial