www.linuxforu.com
|
LINUX FOR YOU
|
MAY 2007
79
verv ew
The list is not exhaustive, yet it covers a few veryimportant reasons to (re)use standard components asmuch as possible.However, just like C++, STL is designed for experiencedprogrammers: it requires some expertise to make use of itsfull potential and there are lots of traps and pitfalls that anovice can easily fall into. Also, it is a generic library whosedesign is inspired by the functional programming paradigm: itis not object-oriented! Hence, you need some understandingand experience about the design philosophy and problem-solving approach of STL to make the best use of it.
STL components
STL consists of three main parts:
Containers (like a stack)
Generic algorithms (like a sort)
Iterators (similar to the use of pointers)These components are designed such that they areignorant of specific details of other components, so thatthey can be combined together as the need arises. Herelies the secret of the power of STL: the ability toseamlessly combine the three to fit our need.Containers are objects that can hold other objects. Wecan use any type of object with these containers, butgenerally, it is assumed that an object that is used with acontainer has the following defined for the object:
copy constructor
assignment operator
== operator
< operatorThis is because whenever an object is used with acontainer, a copy of the object is created and only the copy ispresent in the container; so we need the copy constructorand assignment operator to be defined. Also, many of thealgorithms used by the containers need a comparisonbetween objects; so we need == and < overloaded operators.
Containers
Containers are the data-structures in which we can storeobjects. Basically, we have two kinds of containers:
Sequence containers: Sequence containers: Sequence containers: Sequence containers: Sequence containers:
These containers store andretrieve data in a sequential fashion. They include thesimple
array, list, vector
and
deque.
Associative containers: Associative containers: Associative containers: Associative containers: Associative containers:
The elements of thesecontainers are associated in some manner. Theassociative containers are
map, multimap, set
and
multiset.
They rely heavily on comparison operators,because the objects are stored in a sorted order.In other words, sequence containers can hold elementsof the same type, whereas associative containers arecapable of holding a key-value pair.Let’s look at a few of the containers in STL:
vector: vector: vector: vector: vector: Vector
can be treated as an array with the capabilityof growing or shrinking dynamically. This can be safely usedinstead of arrays. The elements can be accessed in two ways:
using the overloaded operator
[]
using the method
at
The first one is easier and faster to use, but it is not rangechecked. On the other hand, the at method does rangechecking and throws
out_of_range
exception if needed.
deque:deque:deque:deque:deque:
This is basically a double-ended queue. If wewant to grow/shrink in a vector, we can do it only at oneend. But with
deque
, we can do it at both the ends. Itprovides the same accessing efficiency as the
vector
butthe allocation efficiency comparable with a
list
.
list:list:list:list:list:
Arrays are optimised for random access but areinefficient when it comes to inserting and deletingelements in the middle; so are the
vector
and
deque.
Foroperations requiring intensive insertions and deletions, usea list, which is very efficient for these operations (and isinternally implemented as a
double-linked list
). With alist, you cannot have random access to the data and so the
[]
operator is not overloaded.
map and set: map and set: map and set: map and set: map and set:
Both store the elements along with aunique key. In most of the cases, these two areinterchangeable. The only difference is that in a set, the values are irrelevant and we keep track of the keys only.They provide an important functionality, which thesequence containers do not provide—the
find
operation.
multimap and multiset: multimap and multiset: multimap and multiset: multimap and multiset: multimap and multiset:
These are extended versions of a map and set respectively. In a map and set,the keys should be unique. But a multimap and multiset donot have this constraint. All the operations of theirrespective counterparts are supported here also.
Algorithms
The
<algorithm>
header file provides us with many of thealgorithms that we use in our day-to-day programming (andlots of not so obvious ones, too). For example, most of ushave ended up writing our own versions of a sort, search, find,etc, and STL allows us to reuse the code and helps us toconcentrate on more creative aspects of programming. Let uslook at an example of using a fundamental operation—swap:
#include <iostream>#include <algorithm>using namespace std;int main(){string this = “this”, that = “that”;std::swap(this, that);cout<< “this = “<< this<< “and that = “<< that<< endl;}// prints: this = that and that = this
Algorithms in STL are basically of three types:
non-modifying (sequence); for example,
find
modifying (sequence); for example,
fill
sorted (sequence); for example,
sort
Non-modifying algorithms are for read-only/traversingfunctionality that essentially doesn’t modify the content of the containers. In short, they don’t modify the sequence onwhich they operate. Note that ‘sequence’ here refers to the‘sequence containers’—referring to containers with
Leave a Comment