Not long ago, Microsoft released VisualC++ 2008 Feature Pack, a package with extensions to MFC 9.0 and an implementation of TR1 (a document specifying extensions to STL, but not yet standardized). These additions are grouped in 10 new headers under the namespace std::tr1. In this article, I will introduce the header and class array.
The array Type
An array is a sequence of a fixed length (N) of the same type. Once defined, the size of an array cannot be modified. In the following examples, I will use the type int for the elements of the arrays, but it should be obvious, because array is a template class, that any type can be used.
Creating Arrays
An array can be created an initialized in several ways:
- Using the default constructor (without parameters)
// an array with 3 uninitialized elements std::tr1::array<int, 3> arr;
// an array with 3 initialized elements std::tr1::array<int, 3> arr = {1, 2, 3};
// an array with 3 initialized elements std::tr1::array<int, 3> arr1 = {1, 2, 3}; // making copies of the array std::tr1::array<int, 3> arr2(arr1); std::tr1::array<int, 3> arr3 = arr2;
A class array has a method called assign() that sets all the elements of an array to the specified value.
// an array with 3 uninitialized elements std::tr1::array<int, 3> arr3; // set all the elements to 1 arr3.assign(1);
Size of an Array
There are two methods, size() and max_size(), that return the number of elements of an array. There is no difference between the two. Moreover, the method empty() indicates whether an array has no elements (in other words, its dimension is 0).
std::tr1::array<int, 3> arr_d3; std::cout << "size = " << arr_d3.size() << std::endl; std::cout << "empty = " << std::boolalpha << arr_d3.empty() << std::endl; std::tr1::array<int, 0> arr_d0; std::cout << "size = " << arr_d0.size() << std::endl; std::cout << "empty = " << std::boolalpha << arr_d0.empty() << std::endl;
size = 3 empty = false size = 0 empty = true
Iterating and Accessing Elements
You can iterate over an array just like any other STL container.
- Using direct iterators (head to tail)
std::tr1::array<int, 3> arr1 = {1, 2, 3}; for(std::tr1::array<int, 3>::const_iterator it = arr1.begin(); it != arr1.end(); ++it) { std::cout << *it << " "; } std::cout << std::endl;
1 2 3
for(std::tr1::array<int, 3>::const_reverse_iterator rit = arr1.rbegin(); rit != arr1.rend(); ++rit) { std::cout << *rit << " "; } std::cout << std::endl;
3 2 1
Another way to access elements of an array is by using operator[] or method at(). The difference between the two is that method at() throws an exception of type std::out_of_range if the index is not valid.
std::tr1::array<int, 3> arr = {1, 2, 3}; for(size_t i = 0; i < arr.size(); ++i) { std::cout << arr[i] << " "; } std::cout << std::endl;
1 2 3
std::tr1::array<int, 3> arr = {1, 2, 3}; for(size_t i = 0; i < 10; ++i) { try { std::cout << arr2.at(i) << " "; } catch(std::out_of_range& ex) { std::cout << ex.what() << std::endl; } } std::cout << std::endl;
1 2 3 invalid array<T> subscript invalid array<T> subscript invalid array<T> subscript invalid array<T> subscript invalid array<T> subscript invalid array<T> subscript invalid array<T> subscript