670 likes | 819 Views
Introduction to STL. STANDARD TEMPLATE LIBRARY –UNIT 6. INTRODUCTION. Template can be used to create generic classes and functions to support generic programming.
E N D
Introduction to STL STANDARD TEMPLATE LIBRARY –UNIT 6
INTRODUCTION • Template can be used to create generic classes and functions to support generic programming. • Alexander and Meng Lee of HP developed a set of general purpose template classes(data structures) and functions(Algorithm) to store and processing data. • The collection of generic classes and functions called Standard Template Library.
More!... • Using STL we can save lot of time and efforts. • We can design high quality programs. • These are known as Components. • All these components are defined under namespace std. • We must therefore use the directive, using namespace std; to inform the compiler that we intend to use Standard C++ library.
Components of STL • The Components are, • Containers • Algorithms • Iterators .
All Three Works Together!.. CONTAINER ALGORITHM-2 ALGORITHM-1 Object1 Object2 Iterator-1 Iterator-2 Object3 Iterator-3 ALGORITHM-3
Containers • A container is an object that actually stores data. • It is way data is organized in memory. • STL containers are implemented by template classes and can be used to any data types.
Algorithm • Is a procedure that is used to process the data contained in the containers. • The STL includes different kinds of algorithm to provide support to task such as searching, sorting, copying ,merging or initializing. • These are implemented by the template functions.
Iterators • Is an object (like pointer) that points to an element in a container. • We can increment and decrement this Iterators. • Iterators connects algorithm with containers and play a key role in the manipulation of the data stored in the containers.
Containers detail • Containers of three types, • Sequence Containers- vectors ,deque, list • Associate Containers-set , multiset , map, multimap . • Derived containers- stack , queue, priority_queue.
Sequence Containers • Sequence containers stores elements in a linear sequence. • Each element is related to other elements by its position along the line. • They all expands themselves to allow insertion and deletions. E 0 E 1 E 2 E n-1 iterator begin
More!.. • Elements in all these containers can be accessed using an iterator. • The difference between the three of them is only their performance. • Vector– fast access, slow ins &del , fast ins. at back. • list – slow access , fast ins & del , fast ins. At front. • deque – fast access , slow ins. & del , fast at both the ends.
Associate Containers • Associate containers are designed to support direct access to elements using keys. • They are not sequential. • There are 4 types, • set • multiset • map • multimap
More!... • All these containers uses tree structure to store data for fast searching, deletion and insertion. • Slow for sorting and random access. • set can store any number of items and provides operations on these items. • They uses keys to manipulate the data. • Ex: set of students objects and ordered alphabetically by using Name as key.
More!... • set cannot have duplicate items or values. • Multiset can have duplicate items. • Map and multimap is used to store pairs of values one called key and another called value. • map allows one key for one value but multimap allows multiple keys for one value.
Derived containers • Three types of derived containers, • stack • queue • priority_queue
More!... • Stack ,queue and priority_queue can be created from different sequenced containers. • This supports two functions pop() and push().
Algorithms • Algorithm are functions that are used to process the content of the containers. • STL provides more than 60 algorithms and we can use two different containers at same time. • STL algorithms are not member function nor friend function , they are standalone template functions.
More!... • To access algorithm programmer must include header file called < algorithm>. • Categories, • Retrieving algor. • Sorting algor. • Set algor. • Relational Algor.
VECTOR • Vector supports dynamic array. • This is an array that can grow as needed. • template of vector written like this, template<class T, class Allocator=allocator<T>> class vector{ } Here T is type of the data being stored. Allocator specifies the allocator.
More!.. vector has the following constructors: • explicit vector(const Allocator &a = Allocator( ) ); • explicit vector(size_typenum, const T &val = T ( ), const Allocator &a = Allocator( )); • vector(const vector<T, Allocator> &ob); • template <class InIter> vector(InIterstart, InIter end, const Allocator &a = Allocator( ));
More!... • The first form constructs an empty vector. • The second form constructs a vector that has num elements with the value val. • The value of val may be allowed to default. • The third form constructs a vector that contains the same elements as ob. • The fourth form constructs a vector that contains the elements in the range specified by the iteratorsstart and end.
Declaring Vector • Although the template syntax looks rather complex, there is nothing difficult about declaring a vector. • Here are some examples: vector <int> iv; // create zero-length int vector vector <char> cv(5); // create 5-element char vector vector <char> cv(5, 'x'); // initialize a 5-element char vector vector <int> iv2(iv); // create int vector from an int vector
Functions on vector • Some of the most commonly used member functions are size() , begin() , end() , push_back() , insert() , and erase() . • The size() function returns the current size of the vector. • This function is quite useful because it allows you to determine the size of a vector at run time. • Remember, vectors will increase in size as needed, so the size of a vector must be determined during execution, not during compilation.
The begin() function returns an iterator to the start of the vector. • The end() function returns an iterator to the end of the vector. • The push_back() function puts a value onto the end of the vector. • If necessary, the vector is increased in length to accommodate the new element. • You can also add elements to the middle using insert() .
More!.. • You can use array subscripting to access or modify those elements. • You can remove elements from a vector using erase() .
Here p is initialized to point to the start of the vector by using the begin() member function. • This function returns an iterator to the start of the vector. • This iterator can then be used to access the vector an element at a time by incrementing it as needed. • This process is directly parallel to the way a pointer can be used to access the elements of an array. • To determine when the end of the vector has been reached, the end() member function is employed.
vector<char>::iterator p = v.begin(); • p += 2; // point to 3rd element • // insert 10 X's into v • v.insert(p, 10, 'X');
// remove those elements • p = v.begin(); • p += 2; // point to 3rd element • v.erase(p, p+10); // remove next 10 elements
// Insert v2 into v • v.insert(p, v2.begin(), v2.end());
LIST • The list class supports a bidirectional, linear list. • Unlike a vector, which supports random access, a list can be accessed sequentially only. • Since lists are bidirectional, they may be accessed front to back or back to front. • A list has this template specification: • template <class T, class Allocator = allocator<T>> class list { } • Here, T is the type of data stored in the list.
More!... • It has the following constructors: • explicit list(const Allocator &a = Allocator( ) ); • explicit list(size_typenum, const T &val = T ( ), const Allocator &a = Allocator( )); • list(const list<T, Allocator> &ob); • template <class InIter> list( InIterstart, InIter end, const Allocator &a = Allocator( ));
More!.. • The first form constructs an empty list. • The second form constructs a list that has num elements with the value val, which can be allowed to default. • The third form constructs a list that contains the same elements as ob. • The fourth form constructs a list that contains the elements in the range specified by the iteratorsstart and end.