1 / 13

Standard template library

Standard template library. At the core of the STL are three fundamental items: containers, algorithms and iterators. These items work in conjunction with one another to provide off-the-shelf solutions to a variety of programming problems.

sharne
Download Presentation

Standard template library

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Standard template library

  2. At the core of the STL are three fundamental items: containers, algorithms and iterators. These items work in conjunction with one another to provide off-the-shelf solutions to a variety of programming problems. • Containers are objects that hold other objects, and there are several different types- the vector class defines a dynamic array, a deque creates a double ended queue, a list provides a linear list. These containers are sequential containers. • It also defines associative containers which allow efficient retrieval of values based on keys. For example map class. Each container class defines

  3. a set of functions – to insert, delete and merge elements. • Algorithms act on containers, to manipulate the contents of containers. Their capabilities include initialization, sotring, searching and transforming the contents with in a container. • Iterators are objects that are pointers, which gives the ability to cycle through the contents of the container. Five types of iterators are : bidirectional, random access, forward, input, output. • We need to include ‘using namespace std;’ to execute the STL facility.

  4. The standard implementation of C++ does provide a set of header files where a large number of useful class templates have been defined. These files contain definitions of the class templates, their member functions and a number of global associated functions which implement commonly used algorithms. The library of class templates and their helper global functions is known as the standard template library (STL). • The list class :- It is used to create sequential containers. Elements of the list are single objects. Objects of the ‘list’ class are declared as - list <char> clist ; // creating a list of characters list <float> flist ; list <int> ilist ; for using the list template class, the header file ‘list’ needs to be

  5. in the source code. #include<list> • The elements of the list occupy a non-contiguous memory. They are doubly linked through a pair of pointers. One of the pointers point at the next element and the other points at the previous element of the list. Allows both forward and backward traversal. list<int> ilist(3); //list with 3 initial elements • Default values can be specified for these elements list<int> ilist(3, -1); //each element having -1 • A list can be created from an existing array. First parameter will be pointer to the first element and second pointer that points 1 past the last element

  6. of the array to be copied. Int iArr = { 0,1,2,3,4,5 } ;//an array with six elements list<int> ilist(iArr, iArr+6); //list has same values • Following are the important member functions of this class • The list<>::push_front() function:- used to insert the elements at the beginning of the list. list<int> ilist; ilist.push_front(1); • The list<>::push_back() function:- used to insert the element at the end of the list. • The list<>::pop_front() function:- used to delete the first element of the list. • The list<>::pop_back() function:-delete last

  7. Traversing a list using iterator:- an iterator allows us to traverse the list elements in sequence. list <int> ilist ; ilist.push_back(1); ilist.push_back(2); ilist.push_back(3); list<int> :: iterator iter = ilist.begin(); for( ; iter!=ilist.end() ; ++ iter) cout<< *iter<<endl; • The list<>::insert() function:- enables a random insertion to the list. It is used with find() function ilist.insert(ilist.begin(),-20); //inserts at beginning • The find() function:- this global function searches the specified value in the list. If found, it returns an iterator to the element. Else returns the value of the ‘list.end()’ function

  8. list<int> iterator iter; //inserts -1 before 10 iter= find(ilist.begin(),ilist.end(),10); ilist.insert(iter, -1); //appends at end if not found • The list<>::size() function:- to determine the number of elements currently in the list. cout<<ilist.size()<<endl; • The list<>::erase() function:- enables random deletion from the list. The iterator to the position of the element to be deleted is passed to the function. iter= find(ilist.begin(),ilist.end(),19); if(iter!=ilist.end()) ilist.erase(iter) ; • The list<>::clear() function:-clears all elements in a list. ilist.clear();

  9. The list<>::empty() function:- to test whether a list is empty or not. if( ilist.empty() ) //do something else //do something else • Insertion and deletion from an intermediate position in a list is efficient because for such operations only the pointers of the affected element need to be reassigned. • Random access to a particular element is inefficient, because the elements are not in contiguous blocks of memory and hence the pointers of the preceding elements are to be read from the beginning of the list.

  10. The vector class :- it is used to create sequential containers and stores elements in contiguous memory locations and enables direct access to any element using the subscript operator. • The member functions of ‘vector’ are same as those of ‘list’. Find() function have been overloaded in vector class also. ‘#include<vector>’ is to be included for using the class. • The vector object does not actually regrow itself with individual insertion. Instead, it captures an amount of memory larger than the number of elements it actually stores. When this storage becomes full, it regrows to accommodates insertion

  11. Capacity is the total size of the block currently captured by a vector. It is directly proportional to the number of elements that can be inserted. • Size is the number of elements actually stored in the memory block and it is less than or equal to the capacity of the vector. The functions‘vector::size()’ and ‘vector :: capacity()’ are used. • Insertion and deletion is inefficient • Random access to a particular element is efficient • The pair class :- objects of this class represent a pair of values that may or may not be of the same type. pair<string, int> player(“Anand”, 2000); the object ‘player’ represents the name and the number of

  12. games played. We can create more variables of same type using ‘typedef’ typedef pair<string, int> player; player santhosh (“santhosh”, 1000); • For using ‘pair’ template class, the header file ‘utility ‘ needs to be included. • The two elements of the pair class can be accessed as first and second. cout<< “number of games of “ << santhosh.first << “are “ << santhosh.second; • The map class :- it is used to create associative containers. Elements of list are key/value pairs. The map class does not allow duplicates.

  13. Header file ‘map’ needs to be included for using the map template class. Each record in the ‘map’ class is an object of the ‘pair’ class. • The set class :- It is used to create sequential containers. Elements of the list are single objects. A set stores a collection of keys in a sorted manner. The data itself serves as the keys to the set. The set contains the keys in a sorted fashion and duplicates are discarded during insertion. Header file ‘set’ needs to be included for using ‘set’ template class. • The multimap class :-this class allows duplicate key values. • The multiset class :- allows duplicate key values

More Related