standard template libraries
DESCRIPTION
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 ?. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/1.jpg)
Standard Template Libraries
Anjali Agrawal
Prashant Kirtane
![Page 2: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/2.jpg)
2
Beginning STL !
What ought to be in standard C++ Library ?
Everything !!
![Page 3: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/3.jpg)
3
The C++ Standard Library
Memory management Type safe by default supplies functions such as sqrt() Efficient Complete
![Page 4: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/4.jpg)
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.
![Page 5: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/5.jpg)
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.
![Page 6: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/6.jpg)
6
Structure of the library
Contains 5 type of components.– Algorithm– Container– Iterator– Function Object– Adaptor
![Page 7: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/7.jpg)
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.
![Page 8: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/8.jpg)
8
Containers !
Objects
Containers
![Page 9: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/9.jpg)
9
Now What ?
Containers
Objects
How do you access this collection of data?
How do you use them?
![Page 10: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/10.jpg)
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.
![Page 11: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/11.jpg)
11
E.g. Algorithms on C++ arrays
#include <algo.h>
……
int iArr[] = {4, 2, 1, 3};
sort(iArr, iArr+4);
Address of the beginning of the array
Past-the-end address
![Page 12: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/12.jpg)
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
![Page 13: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/13.jpg)
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
![Page 14: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/14.jpg)
14
Algorithms …….
Equal .. Compares the contents of two containers and returns true if all corresponding elements are equal.
Search, copy, swap, sort …..etc
![Page 15: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/15.jpg)
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.
![Page 16: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/16.jpg)
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..
![Page 17: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/17.jpg)
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*
![Page 18: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/18.jpg)
18
E.g. User defined functions ..
sort(cpNames, cpNames+4, bAlphaComp);
Address of the bAlphaComp function
Int bAlphaComp(char * s1, char *s2)
{
return(strcmp(……));
}
![Page 19: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/19.jpg)
19
Container Types !
![Page 20: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/20.jpg)
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.
![Page 21: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/21.jpg)
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;
![Page 22: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/22.jpg)
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];
Returns the no of elements currently in the container
Inserts the value at the back
Overloading [] operator
![Page 23: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/23.jpg)
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
![Page 24: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/24.jpg)
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
![Page 25: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/25.jpg)
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;
![Page 26: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/26.jpg)
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
![Page 27: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/27.jpg)
27
More on Lists …..
[] operator is not defined for lists.
list<int> list1;
list<int> list2;
…..
list1.reverse();
list1.merge(list2);
![Page 28: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/28.jpg)
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.
![Page 29: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/29.jpg)
29
Deques….
#include <deque.h>
….
deque<int> aDeque;
…
aDeque.push_back(1);
aDeque.push_back(2);
aDeque.push_front(3);
![Page 30: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/30.jpg)
30
Till Now … Lets Revise !!!
Containers.– Sequential Containers
• Vectors• Lists• Deques
Algorithms
![Page 31: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/31.jpg)
31
Common Questions ….
What is the difference between Vectors and Deques ?
When to use .. What ?
![Page 32: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/32.jpg)
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.
![Page 33: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/33.jpg)
33
Algorithms use the iterators to act on objects in containers ....
Algorithm
Algorithm
Iterators Containers
![Page 34: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/34.jpg)
34
Types of Iterators !
Input iterator. Output iterator. Forward iterator. Bidirectional iterator. Random-access iterator.
![Page 35: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/35.jpg)
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.
![Page 36: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/36.jpg)
36
Using Iterators !
list <int> iList;
list<int> :: iterator it;List of ints
Iterator to the list-of-ints
![Page 37: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/37.jpg)
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.
![Page 38: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/38.jpg)
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);
![Page 39: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/39.jpg)
39
Specialized Iterators !
Iterator Adapters– Reverse Iterators– Insert iterators– Raw storage iterators
Stream Iterators– Input stream iterators– Output stream iterators
![Page 40: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/40.jpg)
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;
![Page 41: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/41.jpg)
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.
![Page 42: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/42.jpg)
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() ) );
![Page 43: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/43.jpg)
43
Stream Iterators
Allows to treat I/O devices and files as iterators.
Files and I/O devices as arguments to algorithms.
ostream_iterator istream_iterator
Stream Iterators
![Page 44: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/44.jpg)
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
![Page 45: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/45.jpg)
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
![Page 46: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/46.jpg)
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());
![Page 47: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/47.jpg)
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));
![Page 48: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/48.jpg)
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.
![Page 49: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/49.jpg)
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.
![Page 50: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/50.jpg)
50
A map of number-word pairs
Cat1
Dog
Snail
2
3
2 Values
Keys
![Page 51: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/51.jpg)
51
More on Maps…...
typedef map<int, char*, less<int> > map_type;
map_type aMap;
map_type :: iterator it;
aMap.insert(map_type::value_type(1, “Cat”) );
aMap.insert(map_type::value_type(2, “Dog”) );
it = aMap.begin();
![Page 52: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/52.jpg)
52
Multimap
which supports duplicate keys (possibly contains multiple copies of the same key value) and provides for fast retrieval of another type T based on the keys.
![Page 53: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/53.jpg)
53
Set
which supports unique keys (contains at most one of each key value) and provides for fast retrieval of the keys themselves
Similar to map but it stores only keys, there are no values.
![Page 54: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/54.jpg)
54
A Set of Characters …..
c
d
f
fKeys
![Page 55: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/55.jpg)
55
Sets ….
set<int, less<int> > aIntSet(iArr, iArr+4);
set<int, less<int> > :: iterator it;
it = aIntSet.begin();
while(it != aIntSet.end())
cout << *it++ << endl;
Definition for sets ….
Iterator for sets ...
![Page 56: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/56.jpg)
56
More on Sets…..
aIntSet.insert(3);
aIntSet.insert(4);
aIntSet.insert(3);
aIntSet.erase(4);
it = aIntSet.find(3);
Inserts some more ints
No effect, already inserted
Erase a int
Finding matching int in set
![Page 57: Standard Template Libraries](https://reader035.vdocument.in/reader035/viewer/2022062322/568147c0550346895db5045b/html5/thumbnails/57.jpg)
57
Multiset
which supports duplicate keys (possibly contains multiple copies of the same key value) and provides for fast retrieval of the keys themselves.
Same as Set, but multiple instances of the same key.