. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms,...

30
. STL: C++ Standard Library
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    223
  • download

    1

Transcript of . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms,...

Page 1: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

.

STL: C++ Standard Library

Page 2: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Main Ideas

General purpose: generic data structures & algorithms, templates

Flexibility: Allows for many combinations of algorithm-container

Simple & uniform interface:

interface through templates (not inheritence) Efficiency

Page 3: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Components

Containers: data structuresvector, list, map, set, deque

Adaptors: high-level data structure stack, queue, priority_queue

Iterators: allow access into containers Algorithms: base algorithms

sort, copy, search, min, max, … Streams: input/output String

Page 4: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Example

Using STL objects to implement a simplified version of ex2

Data structures: string – represent words map<string,vector<string> >

Associate a word with the array of words that appear after it

Page 5: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Quick Facts

Vector<T>

Implements an array of objects of type T Allows random-access to array Can grow on as needed basis

Map<KeyT,ValueT>

“Associative Array”: maps keys to values Implemented as balanced binary tree

Page 6: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Markov-1.cpp

Page 7: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Extension

Same but using k-order Markov chain Represents the probability of a word after a

prefix of k words

Data structures: list<string> - represent a list of objects

See Markov-k.cpp

Page 8: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Containers

Holds copies of elements Assumes Copy Ctor & operator = The standard defines the interface, not the

implementation Two main classes

Sequential containers: list, vector,....

Associative containers:map, multimap, set ...

Page 9: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Sequential Containers

Maintain a linear sequence of objects

Types of operations Access/inesertion/deletion of

elements at the front/end of sequence Random access to elements

Page 10: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Sequential Containerslist - a linked list of objects

Efficient insertion/deletion in front/end/middle

vector - an extendable sequence of objects Efficient insertion/deletion at end Random access

deque – double-ended queue Efficient insertion/deletion at front/end Random access

Page 11: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Sequential Container Interface

size(), empty() push_front(x), push_back(x) front(), back() pop_front(), pop_back()

vector & deque operator[](int index)

Page 12: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Associative Containers

Maintain a collection of keys Requires order on keys (less-than operator) Keys can be accessed based on their order

(Typical) Implementation: red-black binary trees O(log n) time for access/insert/delete

Page 13: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Associative Containers

Set A set of unique keys

Map Associate a value to key (associative array) Unique value of each key

Multiset

Multimap Same, but allow multiple values

Page 14: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Associative Containers & Order

Associative containers use operator< as default order

We can control order by using our own comparison function

To understand this issue, we need to use function object

Page 15: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Function Objects

A class that implements operator()

Advantages: Use the template mechanism (class versus

function) Enable accumulating information in the

function object

Page 16: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Example

template<class T>

class less {

public:

bool operator()(T& lhs, T& rhs)

{ return lhs < rhs; }

};

less<int> cmp; // declare an object

if( cmp(1,2) )

if( less<int>()(1,2) )

Creates temporary objects, and then call operator()

Page 17: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Function Object Bases

Base classes for the functions objects are defined in STL.

These provide standard names for the arguments and return types.

template<class T>

class less :

public binary_function<T, T, bool>

{

}

Page 18: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Function Object Bases

template< class Arg, class Res>

struct unary_function {

typedef Arg argument_type;

typedef Res return_type;

};

template< class Arg, class Arg2, class Res>

struct binary_function {

typedef Arg first_argument_type;

typedef Arg2 second_argument_type;

typedef Res return_type;

};

Page 19: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Using Comparators

template<class T, class Cmp = less<T> >

class set {

}

set<int> s1;

set<int,less<int> > s2;// same type

set<int,MyComp > s3;

MyComp cmp;

set<int,MyComp > s4(cmp);

Creates a new MyComp object.

Use given MyComp object.

Page 20: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

STL Iterators

Iterators are allow to traverse sequences Methods

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

Page 21: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Iterator Types

Output: write only and can write only once Input: read many times each item Forward supports both read and write Bi-directional support also decrement Random supports random access (just like C

pointer)

Output Input Forward Bi-directional Random

Read x = *i x = *i x = *i x = *i

Write *i = x *i = x *i = x *i = x

Iteration ++ ++ ++ ++, -- ++, --, +, -, +=, -=

Comparison ==, != ==, != ==, != ==, !=, <, >, <=, >=

Page 22: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Iterators & Containers

Bidirectional iterators: list, map, set

Random access iterators: vector, deque

Input/output/forward iterators: iostreams

Page 23: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Iterators and Containers

T::iterator – iterator type for type T

begin() – front of the container

end() – element after last

Container C

Container::iterator i

for( i = C.begin(); i != C.end(); i++)

// do something with *i

Page 24: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Iterators & Streams

Can access iostreams through iterators:istream_iterator<string> in(cin);

istream_iterator<string> endOfInput;

ostream_iterator<string> out(cout);

while( in != endOfInput )

{

string s = *in++;

*out++ = s;

*out++ = “ “;

}

see useStreamIterators.cpp

Page 25: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Sequence Adapters

Adapters of basic containers Very easy, but limited interface

stack<T,Seq> provides push, pop, top, size and empty

queue<T,Seq> also provides back

priority_queue<T,Seq,Cmp> provides same interface as stack uses a compare function object

Page 26: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

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

Page 27: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Algorithms

Most STL algorithms works on sequences Sequences are passed as two iterators:

beginning element element one after last

Algorithms depend on iterator type

not on container type

p q

sequence [p,q)

Page 28: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

Copy

template< class In, class Out>

Out copy(In first, In last, Out res)

{

while (first != last)

*res++ = *first++;

return res;

}

See useCopy.cpp

Page 29: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

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

Page 30: . STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.

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