Thursday, November 15, 2007

What STL ever done for us?

"Well, yes, vectors, I mean obviously the vectors are good ..."
"Don't forget queues Reg, I mean, where would we be without properly organized queues and iterators for any data type ?" General murmurs of agreement
"Yes, alright, apart from vectors and queues ..."
"Sorts Reg - I hated having to code up a new sort routine for every class." Here, here, etc.
"Right. So apart from vectors, queues and associated containers classes, iterators, various useful algorithms and functions, what has the STL ever done for us ?"
"Memory allocators Reg. We can allocate container memory using any scheme we like, and change it without rewriting all the code. It keeps things in order" Reg loses his temper
"Order ? Order ? Oh shut up!"

At the end of this section, the waffle above should start making sense to you, but is unlikely to become more humorous as a result of your studies.

There are three types of sequence containers in the STL. These, as their name suggests, store data in linear sequence. They are the vector, deque and list:

  • vector
  • deque
  • list

To choose a container, decide what sort of operations you will most frequently perform on your data, then use the following table to help you.

Time overhead of operations on sequence containers
Operation Vector Deque List
Access 1st Element Constant Constant Constant
Access last Element Constant Constant Constant
Access "random" element Constant Constant Linear
Add/Delete at Beginning Linear Constant Constant
Add/Delete at End Constant Constant Constant
Add/Delete at "random" Linear Linear Constant

Each container has attributes suited to particular applications. The subsections and code samples below should further clarify when and how to use each type of sequence container.

full version: http://www.pottsoft.com/home/stl/stl.htmlx


0 comments: