TR1 – How to use array class?

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.

[sourcecode language='cpp']
// using array class
void FooTR1Array()
{
std::tr1::array arr = {1,2,3,4,5};

using namespace std;

cout << "Contents of array " < copy( arr.begin(), arr.end(),
ostream_iterator(std::cout,”\t”));
cout << "Size of array - " << arr.size() < cout << "Front of array - " << arr.front() < cout << "Front of back - " << arr.back() < cout << "Array max Size- " << arr.max_size() < cout << "Array [3] - " << arr[3] < cout << "First element using pointer - " << *arr.data() <

// array swapping - both arrays should have same properties and size
std::tr1::array arrNew = {111,222,333,444,555};

cout << "Contents of array 2" < copy( arrNew.begin(), arrNew.end(),
ostream_iterator(std::cout,”\t”));

// swap array
arr.swap( arrNew );

cout << endl<< "Contents of array 1 after swapping with array 2" < copy( arr.begin(), arr.end(),
ostream_iterator(std::cout,”\t”));

cout << endl<< "Contents of array 2 after swapping with array 1" < copy( arrNew.begin(), arrNew.end(),
ostream_iterator(std::cout,”\t”));
}
[/sourcecode]

This entry was posted in Uncategorized. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Angelo Rohit
    Posted August 18, 2009 at 9:41 am | Permalink

    Hey Sharath, that was interesting. Its great to see the C++ standard incorporating ideas from the Boost library. The future looks bright!

  2. Posted August 18, 2009 at 9:46 am | Permalink

    Most of the proposed changes for TR1 has taken from boost library. The ISO committee members are also present in the top of boost.org and also, boost license is totally flexible and better than GNU-GPL. It’s really matters when it’s used with commercial products.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>