Presentation is loading. Please wait.

Presentation is loading. Please wait.

Standard Template Library

Similar presentations


Presentation on theme: "Standard Template Library"— Presentation transcript:

1 Standard Template Library
It is a collection of generic software components and generic algorithms, glued by objects called iterators. It has a large number of non-member functions designed to work on multiple classes of container types. This non-member functions are known as generic algorithms. STL has generic components(containers) which are classes that can, in turn, contain other objects. Iterators are pointer like objects.

2 Generic software components(containers)
STL provides tested and debugged components readily available. They are reusable as a building block for other software development projects. These generic software components are also called containers as they are basically collection of other objects.

3 Following are advantages of these components:
Small in number Generality Efficient, tested, debugged and standardized Portability and reusability Automatically adjusting growth Similar behaviour to built-in types Extensibility There are three types of containers Sequence container(vector,list,deque) Associative sorted container(map,multimap, set,multiset) Adaptive container(stack, queue)

4 Generic Algorithms The algorithms which operate on the software components are designed in such a way that they depend very little on the data structure of the component. We can write our own algorithms in place of generic algorithms. STL contains so many useful algorithms like find(), replace(), merge(), sort(), etc. Being general these algorithms are non-member functions and can be used with all the containers.

5 Iterators Iterators provide a way to traverse the containers.
Programmers can not only define and use iterators like pointers to the component objects, they can dereference them, assign them and so on like pointers. The efficiency of the STL iterators depends heavily on two principles: Open design having iterators open for programmers to manipulate. Address manipulation by which the job is possible to be done in minimum steps.

6 List Class The list class is used to create sequential containers.
Elements of list are single objects. The elements of list occupy non-contiguous memory. They are doubly-linked through a pair of pointers. One of the pointers points at the next element and other points to the previous element of the list. List allows both forward and backward traversal.

7 For using list template class, the header file #include<list> needs to be included in the source code. Declaration of the objects of list class: list <char>clist; //creating list of character list<int>ilist; list <int> ilist(3); // number of elements would have can be specified. list<int> ilist(3,0); //initialize all elements with 0 Array of list: int arr[5] = {1,2,3,4,5}; list<int>ilist(arr,arr+5);

8 Member functions: list<>::push_front() list<int>Ilist Ilist.push_front(4); Ilist.push_front(7); //inserts 7 at the beginning List<>::push_back() ilist.push_back(2); ilist.push_back(3);//inserts 3 at the end of the list List<>::pop_front() & list<>::pop_back() ilist.pop_front(); ilist.pop_back();

9 Traversing a list using iterator:
list<int>::iterator iter; for(iter = ilist.begin(); iter != ilist.end(); ++iter) { cout<<*iter; } list<>::insert() function: Ilist.insert(ilist.end(),67); Ilist.insert(ilist.begin(),12); list<>:: size() function: cout<<ilist.size() // gives number of elements of list

10 Find() function: iter = find(ilist.begin(),ilist.end(),67); ilist.insert(iter,78); Find function searches for the value 67 from the beginning of the list to the end. It inserts value 78 before 67 in the list, if the value is found. Else, it appends 78 at the end of the list. i.e. if value is found in the list, it returns current iterator, else it returns the value of the list::end() function.

11 List::erase function:
this function enables random deletion from the list. iter = find(ilist.begin(),ilist.end(),23); if(iter!=ilist.end()) ilist.erase(iter); List::clear() function this function erase all elements in the list. ilist.clear(); List::empty() function this function is used to test whether a list is empty or not. if(ilist.empty) {} else {}

12 Vector class For using template class, the header file vector needs to be included in the source code. A vector does not actually regrow itself with each individual insertion. The amount of memory a vector captures is larger than the number of elements it actually stores. when this storage becomes full, it again regrows itself by a certain amount of accommodate the latest insertion. the amount by which a vector regrows differs from compiler to compiler.

13 The names of the member functions of the vector class are the same as those of the list class.
The vector class has two functions that enable us to find the capacity and size of the vector. These are vector::capacity() and vector::size(). Capacity is the total size of the block currently captured by a vector. And size is number of elements actually stored in the memory block. In vector elements are stored in contiguous memory (just like an array).

14 Insertion into and deletion from an intermediate position in a vector is inefficient. This is because for such operations all elements starting from the insertion point need to be pushed up or pushed down. random access to a particular element is efficient. For traversing to the element that has our desired value, only the internal iterator has to be incremented since the elements are in the contiguous block of memory.

15 Deque class It is a useful container adapted into the two other containers-the stack and the queue. Deque is a structure where insertion in the end as well as insertion in the front is possible to be done with constant time. ds.push_front(4); this statement is possible with only deque, vector and list only, for other containers it gives compile time error.

16 Sorted associative containers
Sorted associative containers are containers where the content is always sorted; irrespective of the order of insertions and deletions. every element, when inserted, is inserted, is inserted at the exact place where it is to be inserted in the sorted order. we can achieve that ordering overloading operator <() const for each of the objects. these containers are associative and an element of the record can be used to access the entire record.

17 The difference between a sequence container and sorted associative container is that sequence container element is accessed by index or position of element while sorted associative container element is accessed by the value of key, which is a part of element. Sorted associative container also allows sequential operations.


Download ppt "Standard Template Library"

Similar presentations


Ads by Google