Boost and STL

STL introduction: C++ collection

We begin with an introduction on the C++ collection, it’s the bread and butter when programming. The Standard Library is a fundamental part of the C++ Standard. It provides C++ programmers with a comprehensive set of efficiently implemented tools and facilities that can be used for most types of applications. I briefly present the fundamental concepts in the STL. We assume previous knowledge of the basic language features of the C++, in particular, templates (both function templates and class templates).

ÂBelow an example of use of the STL container.

template<typenameRange>void printRange( const Range& aRange)

 

{

std::cout << “We are printing the range\n”;

typedef boost::range_iterator<Range>::type iter_type;

typedef std::iterator_traits<iter_type>::value_type value_type;

std::copy( boost::begin(aRange), boost::end(aRange),

std::ostream_iterator<value_type>(std::cout,“\n”));

}

Same function by using the Standard template Library (STL)

template<typenameCONT> // containervoid print_container( constCONT& aCont2Print)

 

{

// typename because CONT is a argument template

// we have to give an hint to the compiler

typedeftypenameCONT::value_type ctype;

// define an iterator and a const iterator

typedef typename CONT::iterator iter_range;

typedef typename CONT::const_iterator citer_range;

//  initialize range iterators

citer_rangew_beg = aCont2Print.begin();

citer_rangew_end = aCont2Print.end();

std::copy(w_beg,w_end,

std::ostream_iterator<ctype>(std::cout,” “));

std::cout << “\n”;

}

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *