// example.cpp // Program to demonstrate ContainArray classes #include #include #include "ContainArray.h" using std::string; using std::cout; using std::endl; using namespace ContainArray; int main( void ) { // ContainArray provides 1-D, 2-D, and 3-D array classes; they can // be constructed by declaring their length in each dimension Vector v1(3); // vector with 3 rows Matrix m1(2,3); // matrix with 2 rows and 3 columns Tensor t1(4,2,3); // tensor with 4 rows, 2 columns, and 3 layers // We can optionally give an initial value for all elements Vector v2( 3, 1.0 ); // vector filled with 1.0's Vector v3( 3, "el" ); // vector filled with three el's // The elements can be accessed in several ways v1[0] = 1; // with the [] operator v1(1) = 2; // with the () operator v1.at(2) = 3; // with the range-checking at() function // Matrix and Tensor elements are accessed with multiples of these // operators or with operators that take multiple indices m1[0][0] = 11; // first index is row, second index is column m1(0)(1) = 12; // the () operator is equivalent to [] m1.at(0).at(2) = 13; // each index range is checked with at() m1[1][0] = 21; // the [] operator accepts only one index m1(1,1) = 22; // the matrix () operator can take two indices m1.at(1,2) = 23; // as can the range-checking at() function // We can output these classes with the << operator cout << "v1 = " << v1 << endl; // writes the size of the cout << "v2 = " << v2 << endl; // vector followed by the cout << "v3 = " << v3 << endl; // value of each element; cout << "m1 = " << m1 << endl; // likewise for a matrix // A Matrix is actually a Vector of Vectors, so we can assign // a Vector to a row of a Matrix m1[0][0] = v1[0]; // the m1[0][1] = v1[1]; // long m1[0][2] = v1[2]; // way; m1[1] = v1; // the short way cout << "m1 after assigning v1 to each row = " << m1 << endl; // Assigning a value to a Vector sets all elements to that value v1 = 5; cout << "v1 after setting all elements to 5 = " << v1 << endl; m1[0] = 5; cout << "m1 after setting the first row to 5 = " << m1 << endl; // Comparison operators are provided cout << "Row 0 of m1 " << ( m1[0] == v1 ? "equals" : "does not equal" ); cout << " v1" << endl; cout << "Row 1 of m1 " << ( m1[1] != v1 ? "does not equal" : "equals" ); cout << " v1" << endl; // Vectors, Matrixs, and Tensors of the same type can be swapped Vector v4( 4, 7 ); // a vector of four elements, initialized to 7 cout << "v1 = " << v1 << endl; cout << "v4 = " << v4 << endl; v1.swap(v4); cout << "Swapped v1 with v4" << endl; cout << "v1 = " << v1 << endl; cout << "v4 = " << v4 << endl; return 0; }