STL Standard Template Library September 22, 2016 CMPE 250 STL - - PowerPoint PPT Presentation

stl standard template library
SMART_READER_LITE
LIVE PREVIEW

STL Standard Template Library September 22, 2016 CMPE 250 STL - - PowerPoint PPT Presentation

STL Standard Template Library September 22, 2016 CMPE 250 STL Standard Template Library September 22, 2016 1 / 25 STL Standard Template Library Collections of useful classes for common data structures Ability to store objects of


slide-1
SLIDE 1

STL – Standard Template Library

September 22, 2016

CMPE 250 STL – Standard Template Library September 22, 2016 1 / 25

slide-2
SLIDE 2

STL – Standard Template Library

Collections of useful classes for common data structures Ability to store objects of any type (template) Containers form the basis for treatment of data structures Container – class that stores a collection of data STL consists of 10 container classes:

CMPE 250 STL – Standard Template Library September 22, 2016 2 / 25

slide-3
SLIDE 3

STL Containers

Sequence Container

Stores data by position in linear order: First element, second element , etc. All containers

Use same names for common operations Have specific operations

Associate Container

Stores elements by key, such as name, ID number or part number Access an element by its key which may bear no relationship to the location of the element in the container

Adapter Container

Contains another container as its underlying storage structure

CMPE 250 STL – Standard Template Library September 22, 2016 3 / 25

slide-4
SLIDE 4

STL Containers

Sequence Container

Vector Deque List

Adapter Containers

Stack Queue Priority queue

Associative Container

Set, multiset Map, multimap

CMPE 250 STL – Standard Template Library September 22, 2016 4 / 25

slide-5
SLIDE 5

How to access Components - Iterator

Iterator is an object that can access a collection of like objects one

  • bject at a time.

An iterator can traverse the collection of objects. Each container class in STL has a corresponding iterator that functions appropriately for the container For example: an iterator in a vector class allows random access An iterator in a list class would not allow random access (list requires sequential access)

CMPE 250 STL – Standard Template Library September 22, 2016 5 / 25

slide-6
SLIDE 6

Common Iterator Operations

*

Return the item that the iterator currently references

++

Move the iterator to the next item in the list

  • Move the iterator to the previous item in the list

==

Compare two iterators for equality

!=

Compare two iterators for inequality

CMPE 250 STL – Standard Template Library September 22, 2016 6 / 25

slide-7
SLIDE 7

Vector Container

Generalized array that stores a collection of elements of the same data type Vector – similar to an array

Vectors allow access to its elements by using an index in the range from 0 to n-1 where n is the size of the vector

Vector vs array

Vector has operations that allow the collection to grow and contract dynamically at the rear of the sequence

CMPE 250 STL – Standard Template Library September 22, 2016 7 / 25

slide-8
SLIDE 8

Vector Container

Example:

#include <vector> . . . vector<int> scores (100); //100 integer scores vector<Passenger>passengerList(20); //list of 20 passengers

CMPE 250 STL – Standard Template Library September 22, 2016 8 / 25

slide-9
SLIDE 9

Vector Container

Allows direct access to the elements via an index operator Indices for the vector elements are in the range from 0 to size() -1 Example:

#include <vector> vector <int> v(20); v[5]=15;

CMPE 250 STL – Standard Template Library September 22, 2016 9 / 25

slide-10
SLIDE 10

Vector Example

// constructing vectors #include <iostream> #include <vector> using namespace std; int main () { // constructors used in the same order as described above: vector<int> first; // empty vector of ints vector<int> second (4,100); // four ints with value 100 vector<int> third (second.begin(),second.end()); // iterating through second vector<int> fourth (third); // a copy of third // the iterator constructor can also be used to construct from arrays: int myints[] = {16,2,77,29}; vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) ); cout << "The contents of fifth are:"; for (vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it) cout << ’ ’ << *it; cout << ’\n’; return 0; }

CMPE 250 STL – Standard Template Library September 22, 2016 10 / 25

slide-11
SLIDE 11

List Container

Stores elements by position Each item in the list has both a value and a memory address (pointer) that identifies the next item in the sequence To access a specific data value in the list, one must start at the first position (front) and follow the pointers from element to element until data item is located. List is not a direct access structure Advantage: ability to add and remove items efficiently at any position in the sequence

CMPE 250 STL – Standard Template Library September 22, 2016 11 / 25

slide-12
SLIDE 12

STL List Class

Constructors and assignment list <T> v; list <T> v(aList); ll=aList; Access l.front()

returns the first element in the list l.back() returns the last element in the list

CMPE 250 STL – Standard Template Library September 22, 2016 12 / 25

slide-13
SLIDE 13

STL List Class (Cont.)

Insert and Remove l.push_front(value) l.push_back(value) Iterator Delaration list<T>::iterator itr; Iterator Options itr = l.begin()

set iterator to beginning of the list itr = l.end() set iterator to after the end of the list

CMPE 250 STL – Standard Template Library September 22, 2016 13 / 25

slide-14
SLIDE 14

List Example

#include <iostream> #include <list> using namespace std; // Simple example uses type int int main() { list<int> L; L.push_back(0); // Insert a new element at the end L.push_front(0); // Insert a new element at the beginning L.insert(++L.begin(),2); // Insert "2" before position of first argument // (Place before second argument) L.push_back(5); L.push_back(6); list<int>::iterator i; for(i=L.begin(); i != L.end(); ++i) cout << *i << " "; cout << endl; return 0; }

CMPE 250 STL – Standard Template Library September 22, 2016 14 / 25

slide-15
SLIDE 15

Stack Container

Adapter Container These containers restrict how elements enter and leave a sequence Stack

allows access at only one end of the sequence (top) Adds objects to container by pushing the object onto the stack Removes objects from container by popping the stack LIFO ordering (last end, first out)

CMPE 250 STL – Standard Template Library September 22, 2016 15 / 25

slide-16
SLIDE 16

Stack Example

// stack::push/pop #include <iostream> // cout #include <stack> // stack using namespace std; int main () { stack<int> mystack; for (int i=0; i<5; ++i) mystack.push(i); cout << "Popping out elements..."; while (!mystack.empty()) { cout << ’ ’ << mystack.top(); mystack.pop(); } cout << ’\n’; return 0; }

CMPE 250 STL – Standard Template Library September 22, 2016 16 / 25

slide-17
SLIDE 17

Queue Container

Queue

Allows access only at the front and rear of the sequence Items enter at the rear and exit from the front Example: waiting line at a grocery store FIFO ordering (first-in first-out ) push(add object to a queue) pop (remove object from queue)

CMPE 250 STL – Standard Template Library September 22, 2016 17 / 25

slide-18
SLIDE 18

Queue Example

#include <iostream> // cin, cout #include <queue> // queue using namespace std; int main () { queue<int> myqueue; int myint; cout << "Please enter some integers (enter 0 to end):\n"; do { cin >> myint; myqueue.push (myint); } while (myint); cout << "myqueue contains: "; while (!myqueue.empty()) { cout << ’ ’ << myqueue.front(); myqueue.pop(); } cout << ’\n’; return 0; }

CMPE 250 STL – Standard Template Library September 22, 2016 18 / 25

slide-19
SLIDE 19

Priority Queue Container

Priority queue

Operations are similar to those of a stack or queue Elements can enter the priority queue in any order Once in the container, a delete operation removes the largest (or smallest) value

CMPE 250 STL – Standard Template Library September 22, 2016 19 / 25

slide-20
SLIDE 20

Set Container

Set

Collection of unique values, called keys or set members Contains operations that allow a programmer to:

determine whether an item is a member of the set insert and delete items very efficiently

CMPE 250 STL – Standard Template Library September 22, 2016 20 / 25

slide-21
SLIDE 21

Set Example

#include <iostream> #include <set> using namespace std; int main () { set<string> s; cout << "Adding ’Hello’ and ’World’ to the set twice" << endl; s.insert("Hello"); s.insert("World"); s.insert("Hello"); s.insert("World"); cout << "Set contains:"; while (!s.empty()) { cout << ’ ’ << *s.begin(); s.erase(s.begin()); } return 0; }

CMPE 250 STL – Standard Template Library September 22, 2016 21 / 25

slide-22
SLIDE 22

Multi-Set Container

A multi-set is similar to a set, but the same value can be in the set more than once Multi-set container allows duplicates

CMPE 250 STL – Standard Template Library September 22, 2016 22 / 25

slide-23
SLIDE 23

Map Container

Implements a key-value relationship Implements Programmer can use a key to access corresponding values Example: key could be a part number such as X89-21 that corresponds to a part: 912 price and MarsOto manufacturer

CMPE 250 STL – Standard Template Library September 22, 2016 23 / 25

slide-24
SLIDE 24

Multi-map Container

Similar to a map container Multi-map container allows duplicates

CMPE 250 STL – Standard Template Library September 22, 2016 24 / 25

slide-25
SLIDE 25

Writing classes that work with the STL

Classes that will be stored in STL containers should explicitly define the following:

Default constructor Copy constructor Destructor

  • perator=
  • perator==
  • perator<

Not all of these are always necessary, but it might be easier to define them than to figure out which ones you actually need Many STL programming errors can be traced to omitting or improperly defining these methods

CMPE 250 STL – Standard Template Library September 22, 2016 25 / 25