The C++ Standard Template Library (STL) provides a framework for processing algorithms on different kind of containers. However, ordinary arrays don’t provide the interface of STL containers (although, they provide the iterator interface of STL containers). As replacement for ordinary arrays, the STL provides class std::vector. However, std::vector<> provides the semantics of dynamic arrays. It manages data to be able to change the number of elements. This results in some overhead in case only arrays with static size are needed.
New new C++ provides a new class called array which is static in usage but able use a standard C++ container. It also provides access to the underlying data. Thus it’s possible to use as raw array pointers. See the sample snippet below to too see how to use array class. A better documentation can be found at boost::array page.
// using array class
void FooTR1Array()
{
std::tr1::array<int, 5> arr = {1,2,3,4,5};
using namespace std;
cout << "Contents of array " <<endl;
copy( arr.begin(), arr.end(),
ostream_iterator<int>(std::cout,"\t"));
cout << "Size of array - " << arr.size() <<endl;
cout << "Front of array - " << arr.front() <<endl;
cout << "Front of back - " << arr.back() <<endl;
cout << "Array max Size- " << arr.max_size() <<endl;
cout << "Array [3] - " << arr[3] <<endl;
cout << "First element using pointer - " << *arr.data() <<endl;
// array swapping - both arrays should have same properties and size
std::tr1::array<int, 5> arrNew = {111,222,333,444,555};
cout << "Contents of array 2" <<endl;
copy( arrNew.begin(), arrNew.end(),
ostream_iterator<int>(std::cout,"\t"));
// swap array
arr.swap( arrNew );
cout << endl<< "Contents of array 1 after swapping with array 2" <<endl;
copy( arr.begin(), arr.end(),
ostream_iterator<int>(std::cout,"\t"));
cout << endl<< "Contents of array 2 after swapping with array 1" <<endl;
copy( arrNew.begin(), arrNew.end(),
ostream_iterator<int>(std::cout,"\t"));
}