snu oopsla lab. chap17. standard containers © copyright 2001 snu oopsla lab

21
SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab.

Upload: hillary-shepherd

Post on 31-Dec-2015

232 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.

Chap17. Standard Containers

© copyright 2001 SNU OOPSLA Lab.

Page 2: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

Contents

Standard Containers Sequences Sequence Adapters Associative Containers Almost Containers Defining a New Container

Page 3: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

1. Standard Containers Two kinds of containers

Sequences Associative containers

Not fully-developed standard containers Built-in arrays,strings,valarrays,bitsets

Page 4: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

1. Standard Containers

Vector

List

SizeRep

elements Extra space

rep

Elements:

Page 5: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

1.Standard Containers

Map

String

Rep...

nodenode

Rep... Segment descriptors

String segments:

(key,value) pairs:

Page 6: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

1. Standard Containers

Elements in a container are copies of the objects inserted by a copy constructor or an assignment

Associative containers require that their elements can be ordered

Sort(Ran first, Ran last) using < for comparison Sort(Ran first, Ran last, Cmp cmp) using cmp for comparis

on

Page 7: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

2.Sequences

Vector,list,deque : fundamental sequences

Stack,queue,priority_queue

:container adapters or sequence adapters

Page 8: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

2. Sequences

Vector Random-access iterators

List Bidirectional iterators Types and operations like vector’s, except [], at(), capacity(),

and reserve() Additional operations : slice, sort, merge,front, remove, uniq

ue

Page 9: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

2. Sequences Deque

A double-ended queue Operations

List-like efficiency at both ends Vector-like efficiency in the middle

Page 10: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

3. Sequence Adapters

Provides a restricted interface to a container Stack Queue Priority Queue

Page 11: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

3. Sequence Adapters Stack

Defined in <stack>

template <class T, class C=deque<T>> class std::stack {protected:

C c:public:

typedef typename C::value_type value_type;typedef typename C::size_type size_type;typedef C container_type;

explicit stack(const C& a = C()) : c(a){ }bool empty() const { return c.empty(); }size_type size() const {return c.size(); }value_type& top() {return c.back();}const value_type& top() const {return c.back(); }void push { const value_type& x) { c.push_back(x); }void pop() { c.pop_back(); }

}

pass a containeras a template arg

give back,push_back,pop_back their conventional names: top,push,pop

Page 12: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

3. Sequences Adapters Queue

Defined in <queue> An interface to a container that allows an insertion

of elements at the back() and the extraction of elements at the front()

template <class T,class C=deque<T>> class std:queue{…value_type& front(){ return c.front();}const value_type& front() const {return c.front();}

value_type& back(){ return c.back(); }const value_type& back() const { return c.back(); }

}

Page 13: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

3. Sequences Priority Queue

Defined in <queue> A queue in which each element is given a priority

that controls the order

template <class T,class C= vector<T>,class Cmp=less<typename c::value_type>> class std::priority_queue{

…explicit priority_queue(const Cmp& a1=Cmp(), const C& a2=C())

:c(a2),cmp(a1){ }priority_queue(In first, In last, const Cmp&=Cmp(),const C&=CC());

void pop();}

Compare elementsusing the < opby default

return the largest element

Page 14: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

4. Associative Containers

Generalization of the notion of an associative array Associative array

Keeps pairs of values : (key, mapped value) Map,multimap Set, multiset

Page 15: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

4. Associative Containers Map

A sequence of (unique key,value) pairs that provides for fast retrieval based on the key

Bidirectional iterators Type

value_type of a map : (key,value) pair mapped_type : the type of the mapped values

Iteration Simply an iteration over a sequence of pair <const key, mapp

ed_type> Subscripting

Performs a lookup on the key and returns correponding value

Page 16: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

4. Associative Containers Comparisons

By default, < (less than) key_comp(), value_comp()

Map operation find() : find element with key k count() : find number of elements with key k lower_bound() : find first element with key k upper_bound() : find key element with key

greater than k

Page 17: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

4. Associative Containers Multimap

Is like a map ,except that it allows duplicate keys The primary means of accessing multiple values with th

e same key :equal_range(),lower_bound,upper_bound()

Set Can be seen as a map concerning only the keys Rely on a comparison op rather than equlity(==)

Multiset A set that allows duplicate keys

Page 18: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

5. Almost Containers

Hold elements, but lack some aspect of the standard container interface

Built-in array, strings, valarrays, bitsets

basic_string Provides subscripting, random-access iterators, and most of

the notational conveniences of a container Valarray

A vector for optimized numeric computation Provides many useful numeric operations

Page 19: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

5. Almost Containers Bitset

A bitset<N> is an array of N bits with binary conditions Ex)

Constructors Constructed with default values from the bits in an unsign

ed long int, or from a string Bit Manipulation Operations

Binary &(and),| (or), ^(exclusive or),<<(logical left shift),>>(logical right shift), set(),reset(),flip()...

Built-in Arrays Supplies subscripting and random-access iterators in th

e form of ordinary pointers

1 1 0 1 0bitset<5>:

Page 20: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

6. Defining a New Container

A user can design additional containers to fit into the framework provided by the standard containers

Hash_map A map with a hash function The difference between a map and a hash_map

A map requires a < for its element type A hash_map requires an == and a hash function

A first approximation of a hash_map

Template <class Key,class T, class H=Hash<Key>,class EQ=equal_to<Key>, class A = allocator<T>>

class hash_map { //like map, except: typedef H Hasher; typedef EQ key_equal; hash_map(const T& dv=TT(),size_type n=101,const h& hf=H(),const EQ&=EQ()); … hash_map(In first, In last,const TT&dv = T(),size_type n=101,const H& hf=H()); }

Page 21: SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab

SNUOOPSLA Lab.C++

6.Defining a New Container

The key operations The constructors The lookup(operator[]) The resize operation The operation removing an element

Each Entry A key, a value, a pointer to the next entry with

same hash value, and an erased bit

Class hash_map{ …struct Entry {

key_type key;mapped_type val;Entry* next;bool erased;…

} ….

Key val e next

Key val e next

...