introduction to the standard template library (stl) a container class holds a number of similar...
TRANSCRIPT
![Page 1: Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map](https://reader035.vdocuments.us/reader035/viewer/2022072005/56649f425503460f94c617e9/html5/thumbnails/1.jpg)
Introduction to the Standard Template Library (STL)
• A container class holds a number of similar objects.
• Examples:– Vector
– List
– Stack
– Queue
– Set
– Map
![Page 2: Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map](https://reader035.vdocuments.us/reader035/viewer/2022072005/56649f425503460f94c617e9/html5/thumbnails/2.jpg)
Time for Various Operations
![Page 3: Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map](https://reader035.vdocuments.us/reader035/viewer/2022072005/56649f425503460f94c617e9/html5/thumbnails/3.jpg)
Vectors
• Generalization of arrays
• Efficient, random-access to elements
• High-level operations such as increasing/decreasing size of vector
![Page 4: Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map](https://reader035.vdocuments.us/reader035/viewer/2022072005/56649f425503460f94c617e9/html5/thumbnails/4.jpg)
Strings
• In one sense, a vector of characters
• In another sense, a completely different high-level data type
• Lots of string-specific operations (more later)
![Page 5: Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map](https://reader035.vdocuments.us/reader035/viewer/2022072005/56649f425503460f94c617e9/html5/thumbnails/5.jpg)
Lists
• Arbitrary size, memory used efficiently as the list grows and shrinks
• Sequential access only, constant access to first or last element
• Efficient insertion or removal at any location
![Page 6: Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map](https://reader035.vdocuments.us/reader035/viewer/2022072005/56649f425503460f94c617e9/html5/thumbnails/6.jpg)
Deque – Double Ended Queue
• Grows or shrinks as necessary
• Efficient insertion or removal from either end
• Random access to elements
![Page 7: Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map](https://reader035.vdocuments.us/reader035/viewer/2022072005/56649f425503460f94c617e9/html5/thumbnails/7.jpg)
Stacks and Queues
• Special form of deque
• Stack = Last in First out (LIFO)
• Queue = First in First out (FIFO)
![Page 8: Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map](https://reader035.vdocuments.us/reader035/viewer/2022072005/56649f425503460f94c617e9/html5/thumbnails/8.jpg)
Sets
• Ordered collection
• Efficient (logarithmic) insertion, removal, and test for inclusion
• Efficient merge, union, difference, and other set operations
• Multiset allows more than one entry with the same value
![Page 9: Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map](https://reader035.vdocuments.us/reader035/viewer/2022072005/56649f425503460f94c617e9/html5/thumbnails/9.jpg)
Priority Queue
• Efficient (logarithmic) insertion of new values• Efficient access to largest (or smallest) value
– Constant time access
– Logarithmic removal
![Page 10: Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map](https://reader035.vdocuments.us/reader035/viewer/2022072005/56649f425503460f94c617e9/html5/thumbnails/10.jpg)
Map (Dictionary)
• Collection of (key, value) pairs• Keys can be any ordered data type (e.g. string)• Values can be any data type• Efficient insertion, removal, and test for inclusion
![Page 11: Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map](https://reader035.vdocuments.us/reader035/viewer/2022072005/56649f425503460f94c617e9/html5/thumbnails/11.jpg)
Selecting a Container Class
• How are values going to be accessed?– Random – vector or deque
– Ordered – set or map
– Sequential – list
• Is the order in which values are maintained in the collection important?– Ordered – set or map
– Can be sorted – vector or deque
– Insertion time dependant – stack or queue
![Page 12: Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map](https://reader035.vdocuments.us/reader035/viewer/2022072005/56649f425503460f94c617e9/html5/thumbnails/12.jpg)
Selecting a Container Class (cont)
• Will the size of the structure vary widely over the course of execution?– Yes – list or set– No – vector or deque
• Is it possible to estimate the size of the collection?– Yes – vector
![Page 13: Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map](https://reader035.vdocuments.us/reader035/viewer/2022072005/56649f425503460f94c617e9/html5/thumbnails/13.jpg)
Selecting a Container Class (cont)
• Is testing to see whether a value is contained in the collection a frequent operation?– Yes – set
• Is the collection indexed? – Index values are integers – vector or deque– Index values are not integers – map
![Page 14: Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map](https://reader035.vdocuments.us/reader035/viewer/2022072005/56649f425503460f94c617e9/html5/thumbnails/14.jpg)
Selecting a Container Class (cont)
• Can values be related to each another?– Sets require relational operators– Vectors and lists do not require relational
operators
• Is finding and removing the largest (or smallest) value from the collection a frequent operation?– Yes – priority queue
![Page 15: Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map](https://reader035.vdocuments.us/reader035/viewer/2022072005/56649f425503460f94c617e9/html5/thumbnails/15.jpg)
Selecting a Container Class (cont)
• At what positions are items inserted into and removed from the collection?– Middle – list– End – stack or queue
• Is a frequent operation the merging of two or more sequences into one?– Ordered – set– Unordered – list
![Page 16: Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map](https://reader035.vdocuments.us/reader035/viewer/2022072005/56649f425503460f94c617e9/html5/thumbnails/16.jpg)
Container Class - Examples
• Problem: want to read a word and print it out backwards
• Answer: use a stack– Read the word one letter at a time
– Push each letter onto a stack as it is read
– Until the last letter is read
– Pop a letter from the stack and print it out
– Until the stack is empty
![Page 17: Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map](https://reader035.vdocuments.us/reader035/viewer/2022072005/56649f425503460f94c617e9/html5/thumbnails/17.jpg)
Container Class – Examples (cont)
• Pseudocode:
Declare a stack of characters while (there are more characters)
read a character and push it onto the stack while (the stack is not empty)
print the character on top of the stack and pop it off
![Page 18: Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map](https://reader035.vdocuments.us/reader035/viewer/2022072005/56649f425503460f94c617e9/html5/thumbnails/18.jpg)
Container Class – Examples (cont)
#include <iostream>#include <stack>using namespace std; int main(){ char c; stack<char> s; cin.get(c); while (!cin.eof()) { s.push(c); cin.get(c); } while (!s.empty()) { cout << s.top(); s.pop(); }}