Thursday, August 14, 2008

Understanding Data Structures using STL in C++

What is a data structure?

- It’s an abstract object that allows us to use computers efficiently.

There are three important aspects of any data structure:

(i) Abstraction
It’s actions and responses (Functional).

(ii) Implementation
How it is supported by the computer (programming language + compiler).

(iii) Application
Algorithms (what can be done using the abstraction).

Abstraction

There were no Data Structures in assembly. Every program was coded at BIT VECTOR LEVEL(1960s) i.e everything was programmer's responsibility, but today there are embedded systems which will do most of your job with simple microprocessors(thanks to abstraction).At that time there was a basic set of operations like ADD,SHIFT,AND,OR,STORE,RECALL,LABELS,GOTO (for linear(sequential) as well as non-linear execution).

Later on it was also discovered that, on microcoded implementations of certain architectures, complex operations tended to be slower than a sequence of simpler operations doing the same thing.

This gave rise to Reduces Instruction Set Computer (RISC Philosophy)

Links:

http://en.wikipedia.org/wiki/RISC

RISC Vs CISC : http://cse.stanford.edu/class/sophomore-college/projects-00/risc/risccisc/

Data types in C++ (available abstraction)

Basic/Primitives

  • Int

  • Float- for abstracting real numbers

  • Char - ASCII encoding (8 bits with first bit 0 -- 127 characters) and it's indian version ISCII

  • Bool

Complex

  • Linear- Like Arrays etc.

  • Non-linear – Complex abstracted storage mechanism (like sets etc.)

Standard Template Library

Provides :

  • CONTAINERS -- used to store things i.e. act as template for any type.e.g. Vectors, Queues, Stack, List, Sets, Maps .

  • ALGORITHMS

  • ITERATORS (like pointers)

STL CONTAINERS

For details regarding syntax and usage go to http://www.cppreference.com/

Why so many Containers?

Answer: because their performance is different in different situations i.e. some are optimized to do well in some particular situations e.g. vectors are fast for random access but too slow in insertions whereas a list supports fast insertion and deletion but slow random access.

ALGORITHMS

We assume that some basic operations are already present on the template data-type like <,== etc. predefined algorithms in STL work fine with these containers and what they contain within themselves.

There are many algorithms available in STL. Like binary_search, sort, max_element, sort_heap etc. They need specialised pointers (called ITERATORS) for access to each element sequentially.