1 / 57

Standard Template Libraries

Standard Template Libraries . Anjali Agrawal Prashant Kirtane. Beginning STL !. What ought to be in standard C++ Library ? Everything !!. The C++ Standard Library. Memory management Type safe by default supplies functions such as sqrt() Efficient Complete. Why STL ?.

cyrah
Télécharger la présentation

Standard Template Libraries

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 Libraries Anjali Agrawal Prashant Kirtane

  2. Beginning STL ! • What ought to be in standard C++ Library ? Everything !!

  3. The C++ Standard Library • Memory management • Type safe by default • supplies functions such as sqrt() • Efficient • Complete

  4. Why STL ? • Flexibility The use of generic algorithms allows algorithms to be applied to many different structures. • Efficiency STL containers are very close to the efficiency of hand-coded, type-specific containers. • Easy-to-learn structure The library is quite small owing to the high degree of genericity.

  5. Brains ! • Alex Stepanov and Meng Lee of Hewlett-Packard Labs. • July 1994, the ANSI/ISO C++ Standards Committee voted to adopt STL as part of the standard C++ library.

  6. Structure of the library • Contains 5 type of components. • Algorithm • Container • Iterator • Function Object • Adaptor

  7. What are Containers ? • A Container is a way that stored data is organized in memory. • Data may be of built-in types or of class objects. • E.g. Arrays, Stacks, Linked lists.

  8. Containers ! Containers Objects

  9. Now What ? Containers How do you access this collection of data? How do you use them? Objects

  10. Algorithms • Are stand alone functions that performs operations on collections of data(containers). • Are designed to work on STL containers but we can also apply on C++ arrays.

  11. E.g. Algorithms on C++ arrays #include <algo.h> …… int iArr[] = {4, 2, 1, 3}; sort(iArr, iArr+4); Past-the-end address Address of the beginning of the array

  12. The find() Algorithm • Looks for the first element in a container that has a specified value. …. Int iArr[] = {4, 3, 1, 2}; int *iPtr; iPtr = find(iArr, iArr+4, 3); Specified value

  13. The count() Algorithm • Counts the number of elements in a container having a specified value. ……. Int iArr[] = {2, 3, 3, 1}; count(iArr, iArr+4, 3, nVar); Number of counts …. Keeps on adding. Specified value

  14. Algorithms ……. • Equal .. Compares the contents of two containers and returns true if all corresponding elements are equal. • Search, copy, swap, sort …..etc

  15. Function Objects ! • An object of a template class that has a single member function : the overloaded () operator. Sound mysterious ! But its easy to use.

  16. Why do you require Function Objects ? • There are some algorithms which take this function objects as an arguments. Int iArr[] = {2, 4, 1, 3}; sort(iArr, iArr+4, greater<int>()); greater<>() function object sorts array in descending order..

  17. User written functions ... • User written functions in place of function objects. • Required since function objects operate only on basic C++ types and on classes for which appropriate (+, <) operators are defined. • E.g. ‘<‘ is not defined for char*

  18. E.g. User defined functions .. sort(cpNames, cpNames+4, bAlphaComp); Address of the bAlphaComp function Int bAlphaComp(char * s1, char *s2) { return(strcmp(……)); }

  19. Container Types !

  20. Sequence Containers • are objects that store collections of other objects in a strictly linear arrangement. • Stores a set of elements that can be visualized as a line, like houses on a street.

  21. Vectors • provides array-like random access to a sequence of varying length, with constant time insertions and deletions at the end. #include<vector.h> ……… vector<int> aIntVector;

  22. Vectors … member functions Vector<int> aIntVect; aIntVect.push_back(10); aIntVect.push_back(11); for(int j = 0; j < aIntVect.size(), j++) cout << aIntVect[j]; Inserts the value at the back Returns the no of elements currently in the container Overloading [] operator

  23. Vectors … member functions Char *cpChar[] = {“prash”, “is”, “a”, “good”, “boy”}; vector<char *> aCharVect(cpChar, cpChar+5); vector<char *> aEmpVect(5); aCharVect.swap(aEmpVect); Initializing the vector Empty vector of size 5 Swap contents of two vectors

  24. Vectors .. Member functions Int iArr[] = {1, 2, 4, 5}; vector<int> aIntVect(iArr, iArr+4); aIntVect.insert(aIntVect.begin()+2, 3); aIntVect.erase(aIntVect.begin()+2); • Inefficient

  25. List • Doubly linked list. • which provides linear time access to a sequence of varying length, with constant time insertions and deletions anywhere. #include <list.h> ………. List<char> aCharList;

  26. List …. Member functions list<int> aIntList; aIntList.push_back(2); aIntList.push_back(3); aIntList.push_front(1); aIntList.pop_front(); Push items on back Push items on front Pop items off front

  27. More on Lists ….. • [] operator is not defined for lists. list<int> list1; list<int> list2; ….. list1.reverse(); list1.merge(list2);

  28. Deque • Double- ended queue. • which provides random access to a sequence of varying length, with constant time insertions and deletions at both ends • Similar to vector, but can be accessed at either end.

  29. Deques…. #include <deque.h> …. deque<int> aDeque; … aDeque.push_back(1); aDeque.push_back(2); aDeque.push_front(3);

  30. Till Now … Lets Revise !!! • Containers. • Sequential Containers • Vectors • Lists • Deques • Algorithms

  31. Common Questions …. • What is the difference between Vectors and Deques ? • When to use .. What ?

  32. Iterators • Pointer-like entities used to access individual data items in a container. • Used to move sequentially from element to element called iterating through the container.

  33. Algorithms use the iterators to act on objects in containers .... Containers Iterators Algorithm Algorithm

  34. Types of Iterators ! • Input iterator. • Output iterator. • Forward iterator. • Bidirectional iterator. • Random-access iterator.

  35. Iterators as an interface • Decides which algorithm can be used with which container. • E.g. To be efficient, the reverse() algorithm needs to iterate backward as well as forward through a container.

  36. Using Iterators ! list <int> iList; list<int> :: iterator it; List of ints Iterator to the list-of-ints

  37. More on Iterators….. int iArr[] = {1, 2, 3, 4}; list<int> iList(iArr, iArr+4); list<int> :: iterator it; for(it = iList.begin(); it != iList.end(); it++) cout << *it << endl; • Iterator required since “list” doesn’t support random access.

  38. Algorithms and Iterators ! • Algorithms can take iterators as its arguments. list<int> iList; list<int> :: iterator it; it = find(iList.begin(), iList.end(), 8);

  39. Specialized Iterators ! • Iterator Adapters • Reverse Iterators • Insert iterators • Raw storage iterators • Stream Iterators • Input stream iterators • Output stream iterators

  40. Reverse Iterators ! • Allows to move backwards in the container. List<int> iList(iArr, iArr+4); list<int> :: reverse_iterator rit; rit = iList.rbegin(); while(rit != iList.rend()) cout << *rit++ << endl;

  41. Insert Iterators ! • Allows the data to be inserted without overwriting the existing data. • back_inserter .. Inserts new items at the end. • front_inserter .. Inserts new items at the beginning. • inserter .. Inserts new items at a specified location.

  42. E.g. Insert Iterator copy(d1.begin(), d1.end(), back_inserter(d2)); copy(d1.begin(), d1.end(), front_inserter(d2)); copy(d1.begin(), d1.end(), inserter(d2, d2.begin() ) );

  43. Stream Iterators • Allows to treat I/O devices and files as iterators. • Files and I/O devices as arguments to algorithms. Stream Iterators ostream_iterator istream_iterator

  44. The ostream_iterator Class ! • An ostream_iterator object can be used as an argument to any algorithm that specifies an output iterator. ostream_iterator<int> ositer(cout, “--”); …… copy(iList.begin(), iList.end(), oister); Stream to write

  45. ostream_iterator … to a file ! ofstream outfile(“iter.data”); ostream_iterator<int> ositer(outfile, “ “); copy(iList.begin(), iList.end(), ositer); Create file object Defining Iterator …. Write List to file

  46. The istream_iterator class • An istream_iterator object can be used as an argument to any algorithm that specifies an input iterator. istream_iterator<float, ptrdiff_t> cit(cin); istream_iterator<float, ptrdiff_t> end_of_stream; …….. copy(cit, end_of_stream, fList.begin());

  47. istream_iterator …from a file ! ifstream infile(“iter.dat”); istream_iterator<int, ptrdiff_t> file_iter(infile); istream_iteratot<int, ptrdiff_t> end_of_stream; copy(file_iter, end_of_stream, back_inserter(iList));

  48. Associative Containers • provide for fast retrieval of objects from the collection based on keys. • Is not sequential, instead it uses keys to access data. • Sets and Maps.

  49. Map • supports unique keys (contains at most one of each key value) and provides for fast retrieval of another type T based on the keys. • The keys are arranged in sorted order.

  50. A map of number-word pairs Cat Keys 1 2 2 Dog Values 3 Snail

More Related