Subscripting with Character Strings
The form of a character string subscript is x:y:z. This selects elements x through y, with an increment of z. If the last part, :z, is left out, then all the elements from x through y are selected. Here is an example using the class RWMathVec<T>:
 
RWMathVec<int> v = "[0 1 2 3 4 5 6]";
cout << v("0:2"); // prints [0 1 2]
cout << v("3:1"); // prints [3 2 1]
cout << v("0:6:2"); // prints [0 2 4 6]
 
Any of the parts of the subscript can be omitted. Thus 3: selects all elements from the third to the last, :2 selects all elements up to and including the second element, ::2 selects every other element starting with the first, 1::2 selects every other element starting with the second, and : selects all elements. Here is how to set up a vector containing an alternating sequence of 1s and 0s:
 
RWMathVec<int> v(10);
v("::2") = 1;
v("1::2") = 0; // v = [ 1 0 1 0 1 0 1 0 1 0 ]
A subscripting operation always produces a new view of existing data. In the examples thus far, this new view is temporary and unnamed. It is just as easy to create new named views:
 
RWGenMat<double> A(3,3);
RWGenMat<double> Atl = A("0:1","0:1");
Here the matrices A and Atl are alternative views of the same data. Since Atl is an alias for the data in the top left corner of the matrix A, the following statement changes A as well as Atl:
 
Atl = 0; // sets upper left corner of A to zero
Just as with copy constructors, you can avoid this aliasing property by using the copy() and deepenShallowCopy() member functions.
If you examine the overloaded declarations for operator() in the vector, matrix, and array header files, you will see that none of them take character strings as arguments. This is because subscripting is not done with the character strings themselves. Instead, temporary objects of type RWSlice are constructed automatically by C++ with the constructor RWSlice(const char*), and these temporary objects are used by the subscripting operation. If you use an index more than once, it may be helpful to construct the RWSlice object explicitly. For example, the following segment of code sets up an 8 x 8 matrix with a chessboard pattern where white squares contain a 1 and black squares contain a -1.
 
RWGenMat<double> board(8,8, rwUninitialized);
RWSlice I("0::2"); // selects odd elements
RWSlice J("1::2"); // selects even elements
board(I,I) = board(J,J) = 1; // sets white squares
board(I,J) = board(J,I) = -1; // sets black squares