STL: C++ Standard Library (continued) .

Download Report

Transcript STL: C++ Standard Library (continued) .

.

STL: C++ Standard Library (continued)

STL Iterators

 Iterators are allow to traverse sequences  Methods    operator* operator-> operator++, and operator —  Different types of iterators - to support read, write and random access  Containers define their own iterator types  Changing the container can invalidate the iterator

Iterator Types

Read Output Input Forward Bi-directional Random x = *i x = *i x = *i x = *i Write *i = x *i = x *i = x *i = x Iteration Comparison ++ ++ ++ ==, != ==, != ++, -- ==, != ++, --, +, -, +=, -= ==, !=, <, >, <=, >=

Output

: write only and can write only once 

Input

: read many times each item 

Forward:

supports both read and write 

Bi-directional:

supports also decrement 

Random:

pointer) supports random access (just like C

Iterators & Containers

Bidirectional iterators:  list, map, set Random access iterators:  vector, deque Input/output/forward iterators:  iostreams

Iterators & Containers

Every STL container T provides:

class T { … typedef … iterator

// iterator type T

iterator begin();

// first element of the container

iterator end(;

// element after last of the container };

Iterators & Containers

Typical code will look like:

Container C … Container::iterator i for( i = C.begin(); i != C.end(); i++) // do something with *i

Iterators & Containers

Iterators allow to access/modify elements

Container C; Container::iterator i,j;

C.insert(i,x)

– insert

x

before

i

C.insert(i,first,last)

– insert elements in

[first,last)

C.erase(i)

– erase element

i

before points to

i

C.erase(i,j)

– erase elements in range

[i,j)

Iterator validity

 When working with iterators, we have to remember that their validity can change Whats wrong with this code?

Container C; C::iterator i; for( i = C.begin(); i != C.end(); i++ ) if( f( *i ) ) // some test C.erase(i); //

remove this element

Iterator validity

Second try…

Container C; C::iterator i = C.begin(); while( i != C.end() ) { C::iterator j = i++; if( f( *j ) ) // some test C.erase(j); //

remove this element

}

Works for set, map, list, not vector or deque

Iterators and Map

Suppose we work with

map dictionary; map::iterator i; … i = dictionary.begin();

What is the type of

*i

?

Iterators and Map

Every STL container type Container defines

Container::value_type

Type of elements stored in container  This is the type returned by an iterator

Container::value_type Container::iterator operator*();

Iterators and Map

 Ok, so what type of elements does a map return?

Recall 

map

keeps pairs 

KeyType key

– “key” of entry 

ValueType value

– “value” of entry

Pairs

template< class T1, class T2> struct pair { typedef T1 first_type; typedef T2 first_type; T1 first; T2 second; }; pair( const T1& x, const T2& y ) : first(x), second(y) {}

Map value_type

template< class Key, class T, class Cmp = less > class map { public: typedef pair value_type; typedef Key key_type; typedef T mapped_type; }; typedef Cmp key_compare;

Using map iterator

map dict; … map::iterator i; for( i = dict.begin(); i != dict.end(); i++ ) { cout << i->first << “ “ << i->second << “\n”; } [See dictionary.cpp]

Iterators and Assoc. Containers

Additional set of operations: 

iterator C::find(key_type const& key)

Return iterator to first element with

key

. Return

end()

if not found 

iterator C::lower_bound(key_type const& key)

Return iterator to first element greater or equal to

key

iterator C::upper_bound(key_type const& key)

Return iterator to first element greater than

key

Iterators & Streams

Can access iostreams through iterators:

istream_iterator in(cin); istream_iterator endOfInput; ostream_iterator out(cout); while( in != endOfInput ) { string s = *in++; *out++ = s; *out++ = “ “; }

see useStreamIterators.cpp

Inserters

istream_iterator in(cin); istream_iterator endOfInput; vector vect; back_insert_iterator > back(vect); // copy input words into vector… while( in != endOfInput ) *back++ = *in++; // same as: vect.push_back(*in++)

Inserters

Inserters are output iterators 

back_insert_iterator( C& c)

Insert at back of

c

front_insert_iterator( C& c)

Insert at front of

c

insert_iterator(C& c, Iter i)

Insert at just before

i

Allow to write into containers in generic algorithms

Do-it-yourself iterators

 You can create iterators Check list:    Define the appropriate operators Ensure copy constructor/operator Define the right typedefs use inheritance from iterator<..> class [See TokenIterator.h]

Sequence Adapters

 Adapters of basic containers  Very easy, but limited interface

stack

 provides

push, pop, top, size

and

empty

queue  also provides

back

priority_queue  provides same interface as stack  uses a compare function object

Container summary

Front ops vector list deque stack queue priority_queue map set [] const const log(n) List ops n+ const n log(n)+ log(n)+ const const const log(n) Back ops const+ const const const const log(n) Iterators Random Bi-direct Random Bi-direct Bi-direct

Algorithms Overview

 Sequence operations  Non modifying:

for_each, find, count, search, mismatch, equal

 Modifying:

transform, copy, swap, replace, fill, generate, remove, unique, reverse, rotate, random_shuffle

 Sorted sequences operations 

sort, lower_bound, upper_bound, equal_range, binary_search, merge, includes, set_union, intersection, difference, symmetric_difference

Algorithms

 Most STL algorithms works on sequences  Sequences are passed as two iterators:   beginning element element one after last p q sequence [p,q)  Algorithms depend on iterator type not on container type

Copy

template< class In, class Out> Out copy(In first, In last, Out res) { while (first != last) *res++ = *first++; return res; }

See useCopy.cpp

Non-modifying Sequence Algorithms

In find(In first, In last, const T& val)

find the first occurence of val in the sequence 

In find_if(In first, In last, Pred p)

find the first element satisfying p 

I1 find_first_of(I1 f1, I2 l1, I2 f2, I2 l2)

find the first match between two sequences.

I1 search( I1 f1, I1 l1, I2 f1, I2 l2 )

search for the sequence

f2...l2

inside

f1..l1

Sorted Sequence Algorithms

sort(In first, In last[, class cmp])

find the first occurence of val in the sequence 

In lower_bound(In first, In last, T const & val[, class cmp])

find the first element not less than

val

bool binary_search(In first, In last, T const & val[, class cmp])

check if

val

appears in the sequence 

Out merge( I1 f1, I1 l1, I2 f1, I2 l2, Out out )

merge two sorted sequences and write the merged sequence onto the output iterator

out

Ex4: Interactive Graph Operations

 An interactive shell that allows to perform graph operations

Gcalc> G1 = {x11, x22 | } Gcalc> G2 = {x11, x22 | } Gcalc> G3 = G1*G2 Gcalc> print G3

Ex5 Preview

Extend the graph calculator  Weighted graphs  Algorithms: shortest path, minimum spanning tree, max flow, etc.

 Load/Save file

Ex4: Components

 Graph data structure  Symbol Table  Command parser  Command evaluater Emphasis: extendibility of command syntax for next exercise

Graph Data Structure

Your design show allow to  Constructors/copy etc.

 Add vertices/edges  Iterate over vertices  Iterate over parents of a vertex  Iterate over children of a vertex  Graph operations