570 likes | 771 Views
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 ?.
E N D
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 ? • 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.
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.
Structure of the library • Contains 5 type of components. • Algorithm • Container • Iterator • Function Object • Adaptor
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.
Containers ! Containers Objects
Now What ? Containers How do you access this collection of data? How do you use them? Objects
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.
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
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
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
Algorithms ……. • Equal .. Compares the contents of two containers and returns true if all corresponding elements are equal. • Search, copy, swap, sort …..etc
Function Objects ! • An object of a template class that has a single member function : the overloaded () operator. Sound mysterious ! But its easy to use.
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..
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*
E.g. User defined functions .. sort(cpNames, cpNames+4, bAlphaComp); Address of the bAlphaComp function Int bAlphaComp(char * s1, char *s2) { return(strcmp(……)); }
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.
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;
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
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
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
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;
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
More on Lists ….. • [] operator is not defined for lists. list<int> list1; list<int> list2; ….. list1.reverse(); list1.merge(list2);
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.
Deques…. #include <deque.h> …. deque<int> aDeque; … aDeque.push_back(1); aDeque.push_back(2); aDeque.push_front(3);
Till Now … Lets Revise !!! • Containers. • Sequential Containers • Vectors • Lists • Deques • Algorithms
Common Questions …. • What is the difference between Vectors and Deques ? • When to use .. What ?
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.
Algorithms use the iterators to act on objects in containers .... Containers Iterators Algorithm Algorithm
Types of Iterators ! • Input iterator. • Output iterator. • Forward iterator. • Bidirectional iterator. • Random-access iterator.
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.
Using Iterators ! list <int> iList; list<int> :: iterator it; List of ints Iterator to the list-of-ints
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.
Algorithms and Iterators ! • Algorithms can take iterators as its arguments. list<int> iList; list<int> :: iterator it; it = find(iList.begin(), iList.end(), 8);
Specialized Iterators ! • Iterator Adapters • Reverse Iterators • Insert iterators • Raw storage iterators • Stream Iterators • Input stream iterators • Output stream iterators
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;
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.
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() ) );
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
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
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
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());
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));
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.
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.
A map of number-word pairs Cat Keys 1 2 2 Dog Values 3 Snail