Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab."— Presentation transcript:

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

2 SNU OOPSLA Lab. C++ Contents Standard Containers Sequences Sequence Adapters Associative Containers Almost Containers Defining a New Container

3 SNU OOPSLA Lab. C++ 1. Standard Containers Two kinds of containers Sequences Associative containers Not fully-developed standard containers Built-in arrays,strings,valarrays,bitsets

4 SNU OOPSLA Lab. C++ 1. Standard Containers Vector List Size Rep elementsExtra space rep Elements:

5 SNU OOPSLA Lab. C++ 1.Standard Containers Map String Rep... node Rep... Segment descriptors String segments: (key,value) pairs:

6 SNU OOPSLA 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 comparison

7 SNU OOPSLA Lab. C++ 2.Sequences Vector,list,deque : fundamental sequences Stack,queue,priority_queue :container adapters or sequence adapters

8 SNU OOPSLA 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, unique

9 SNU OOPSLA Lab. C++ 2. Sequences Deque A double-ended queue Operations List-like efficiency at both ends Vector-like efficiency in the middle

10 SNU OOPSLA Lab. C++ 3. Sequence Adapters Provides a restricted interface to a container Stack Queue Priority Queue

11 SNU OOPSLA Lab. C++ 3. Sequence Adapters Stack Defined in template > 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 container as a template arg give back,push_back, pop_back their conventional names : top,push,pop

12 SNU OOPSLA Lab. C++ 3. Sequences Adapters Queue Defined in An interface to a container that allows an insertion of elements at the back() and the extraction of elements at the front() template > 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(); } }

13 SNU OOPSLA Lab. C++ 3. Sequences Priority Queue Defined in A queue in which each element is given a priority that controls the order template,class Cmp=less > 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 elements using the < op by default return the largest element

14 SNU OOPSLA 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

15 SNU OOPSLA 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 Subscripting Performs a lookup on the key and returns correponding value

16 SNU OOPSLA 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

17 SNU OOPSLA Lab. C++ 4. Associative Containers Multimap Is like a map,except that it allows duplicate keys The primary means of accessing multiple values with the 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

18 SNU OOPSLA 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

19 SNU OOPSLA Lab. C++ 5. Almost Containers Bitset A bitset is an array of N bits with binary conditions Ex) Constructors Constructed with default values from the bits in an unsigned long int, or from a string Bit Manipulation Operations Binary &(and),| (or), ^(exclusive or), >(logical right shift), set(),reset(),flip()... Built-in Arrays Supplies subscripting and random-access iterators in the form of ordinary pointers 1 1 0 1 0 bitset :

20 SNU OOPSLA 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 EQ=equal_to, class A = allocator > 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()); }

21 SNU OOPSLA 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...


Download ppt "SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab."

Similar presentations


Ads by Google