Friday, October 27, 2006

Standard Template Library

For the past few days, I'm learning STL. The good think is that I'm reading it continuously without break and working out the examples chapter by chaper.

STL in a Nutshell
============
STL is a powerful library. It adds more PERL power to C++. STL is an elegant and generic solution. STL have 3 major components :

* Container
* Algorithms
* Iterator

Containers
=======
Stores and retrives data. Examples:

* Simple Array
* Vector (dynamic array)
* Deque (double ended queue)
* List (dohbly linked list)
* Sets & Multisets
* Maps & Multimaps

Algorithms
========
* Copy
* Sort
* Min/Max
* etc ... (You name it, everything is here, expect graph algorithms)

Iterator
========
Thanks to Wikipedia
* input iterator
* output iterator
* forward iterator (sets & maps)
* bi-directional iterator
* Random access iterator

It took me around 1 hr correct the problem in the following code

template <typename>
void display(ttype v) {
typedef typename ttype::iterator v_itr;
cout<<"Element in v :";
for(v_itr i=v.begin();i != v.end();++i)
cout<<*i<<" ";
cout<<endl;
}

I was not able to declare the iterator for the generic type ttype. Most of the programmers are used to

for(int i=0;i<x;i++)

We use i++, postfix increment operator. In some compiler implementation i++ consumes more cpu cycles/memory read than ++i (prefix). The problem is we dont understand i++, when i++ stands alone there is no use. i++ must be used in expressions/assignments.

No comments: