1 / 67

Introduction to STL

Introduction to STL. STANDARD TEMPLATE LIBRARY –UNIT 6. INTRODUCTION. Template can be used to create generic classes and functions to support generic programming.

hunter
Download Presentation

Introduction to STL

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. Introduction to STL STANDARD TEMPLATE LIBRARY –UNIT 6

  2. 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.

  3. 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.

  4. Components of STL • The Components are, • Containers • Algorithms • Iterators .

  5. All Three Works Together!.. CONTAINER ALGORITHM-2 ALGORITHM-1 Object1 Object2 Iterator-1 Iterator-2 Object3 Iterator-3 ALGORITHM-3

  6. 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.

  7. 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.

  8. 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.

  9. Five types

  10. Containers detail • Containers of three types, • Sequence Containers- vectors ,deque, list • Associate Containers-set , multiset , map, multimap . • Derived containers- stack , queue, priority_queue.

  11. More!>..

  12. 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

  13. 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.

  14. 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

  15. 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.

  16. 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.

  17. Derived containers • Three types of derived containers, • stack • queue • priority_queue

  18. More!... • Stack ,queue and priority_queue can be created from different sequenced containers. • This supports two functions pop() and push().

  19. 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.

  20. More!... • To access algorithm programmer must include header file called < algorithm>. • Categories, • Retrieving algor. • Sorting algor. • Set algor. • Relational Algor.

  21. Algorithms in STL

  22. 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.

  23. 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( ));

  24. 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.

  25. 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

  26. 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.

  27. 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() .

  28. More!.. • You can use array subscripting to access or modify those elements. • You can remove elements from a vector using erase() .

  29. Accessing data through Iterator

  30. 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.

  31. vector<char>::iterator p = v.begin(); • p += 2; // point to 3rd element • // insert 10 X's into v • v.insert(p, 10, 'X');

  32. // remove those elements • p = v.begin(); • p += 2; // point to 3rd element • v.erase(p, p+10); // remove next 10 elements

  33. // Insert v2 into v • v.insert(p, v2.begin(), v2.end());

  34. 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.

  35. 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( ));

  36. 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.

More Related