Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:

Similar presentations


Presentation on theme: "CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:"— Presentation transcript:

1 CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures: store values according to a specific organization –Iterators Variables used to give flexible access to the values in a container –Algorithms Functions that use iterators to access values in containers Perform computations that modify values, or creates new ones –Function objects Encapsulate a function as an object, use to modify an algorithm The STL makes use of most of what we’ve covered –Functions, classes, structs –Extensive use of function and class templates, concepts –Typedefs, traits, and associated types

2 CSE 332: C++ STL containers Intro to C++ STL Containers Goals –See how STL containers are implemented –See how differences in implementation affect use We’ll cover several kinds –Focus on template concepts –And how each container’s concept relates to its implementation Example to the left prints v[0] is 1 v[1] is 2 v[2] is 4 #include using namespace std; int main (int, char *[]) { vector v; // This would be asking for trouble.... // v[0] = 1; v[1] = 2; v[2] = 4; //... but this works fine... v.push_back (1); v.push_back (2); v.push_back (4); //... and now this is ok... for (size_t s = 0; s < v.size(); ++s) { cout << "v[" << s << "] is " << v[s] << endl; } return 0; }

3 CSE 332: C++ STL containers template struct block{ typedef T value_type; // basic associated types typedef const T const_value_type; typedef value_type * pointer; typedef value_type & reference; typedef const_value_type* const_pointer; typedef const_value_type & const_reference; typedef size_t size_type; …… typedef pointer iterator; // iterator associated types typedef const_pointer const_iterator; …… }; From Matthew H. Austern, “Generic Programming and the STL” Example: Making a STL Container from an Array

4 CSE 332: C++ STL containers template struct block{ …… iterator begin() {return data;} // iterator accessors iterator end() {return data+N;} const_iterator begin() const {return data;} const_iterator end() const {return data+N;} …… reference operator [ ] (size_type n) // container interface {return data[n];} const_reference operator [ ] (size_type n) const {return data[n];} size_type size() const {return N;} T data [N]; // contained elements }; Array-based STL Container Example, Continued

5 CSE 332: C++ STL containers Basic Requirements for an STL Container Contains elements of a parameterized type –Sets/ranges of contained elements cannot overlap –Location cannot belong to more than one container Adding and copying elements is by value –Container owns the elements Container’s destructor destroys what it contains Provides interfaces to contained values –Functions to add/remove values, e.g., push_back –Operations, e.g. operator[] for random access –Should obey the “principle of least surprise” For example a linked list should not provide []

6 CSE 332: C++ STL containers Containers Must Also Work with Iterators Declare appropriate iterator types (traits) typedef pointer iterator; typedef const_pointer const_iterator; Provide iterator accessor/factory methods iterator begin() {return data;} iterator end() {return data+N;} const_iterator begin() const {return data;} const_iterator end() const {return data+N;} iterator rbegin() {return data+N-1;} iterator rend() {return data-1;} const_iterator rbegin() const {return data+N-1;} const_iterator rend() const {return data-1;} More about iterators next lecture

7 CSE 332: C++ STL containers Container Forward Container Reversible Container Random Access Container Sequence Front Insertion Sequence Back Insertion Sequence Container Simple Associative Container Pair Associative container Unique Associative Container Sorted Associative Container Multiple Associative Container Hashed Associative Container Hierarchy of STL Container Concepts Associative From: Matthew H. Austern, “Generic Programming and the STL”

8 CSE 332: C++ STL containers Container Forward Container Reversible Container Random Access Container Sequence Front Insertion Sequence Back Insertion Sequence General Container Concepts refined by models vector deque list Notice containers can have multiple classifications –Useful to look at differences between data structures! –Back vs. front insertion –Forward vs. reversible vs. random access More general concepts higher in the hierarchy More specific concepts appear farther down slist

9 CSE 332: C++ STL containers Container: Top of its Concept Hierarchy Valid ExpressionsComplexity –Copy constructor X(a) linear –Copy constructor X b(a) linear –Assignment operator b = a linear –Destructor a.~X() linear –Beginning of range a.begin() constant –End of range a.end() constant –Size a.size() container-dependent –Maximum size a.max_size() constant –Empty a.empty() constant –Swap a.swap(b) linear Container

10 CSE 332: C++ STL containers Forward Container Additional ExpressionsComplexity –Equality a == b linear –Inequality a != b linear –Less than a < b linear –Greater than a > b linear –Less than or equal a <= b linear –Greater than or equal a >= b linear Container Forward Container vector deque list slist

11 CSE 332: C++ STL containers Reversible Container Additional Expressions Complexity –Beginning of reverse range a.rbegin() constant –End of reverse range a.rend() constant vector deque list Container Forward Container Reversible Container

12 CSE 332: C++ STL containers Random Access Container Additional ExpressionsComplexity –Element access a[n] constant Container Forward Container Reversible Container Random Access Container vector deque

13 CSE 332: C++ STL containers Sequence Additional ExpressionsComplexity –Fill constructor X(n,t) linear –Fill constructor X(n) linear –Default constructor X() linear –Range constructor X(i,j) linear –Insert a.insert(p,t) sequence-dependent –Fill insert a.insert(p,n,t) linear –Range insert a.insert(p,i,j) linear –Erase a.erase(p) sequence-dependent –Range erase a.erase(p,q) linear –Front a.front() constant Container Forward Container Sequence vector deque listslist

14 CSE 332: C++ STL containers Front Insertion Sequence Additional ExpressionsComplexity –Push front a.push_front(t) constant –Pop front a.pop_front(t) constant Container Forward Container Sequence Front Insertion Sequence deque list slist

15 CSE 332: C++ STL containers Back Insertion Sequence Additional ExpressionsComplexity –Back a.back() constant –Push back a.push_back(t) constant –Pop back a.pop_back(t) constant vector deque list Container Forward Container Sequence Back Insertion Sequence

16 CSE 332: C++ STL containers Container Simple Associative Container Pair Associative container Unique Associative Container Sorted Associative Container Multiple Associative Container (Hashed Associative Container) Associative Container Concepts Associative Container Forward Container set multiset map multimap (hash_set) (hash_multiset) (hash_map) (hash_multimap) refined by models

17 CSE 332: C++ STL containers Associative Container Additional Expressions Complexity –Default constructor X () constant –Default constructor X a; constant –Find a.find(k) logarithmic –Count a.count(k) O(log(size())+count(k)) –Equal range a.equal_range(k)) logarithmic –Erase key a.erase(k) O(log(size())+count(k)) –Erase element a.erase(p) constant –Erase range a.erase(p,q) O(log(size())+count(k)) set multiset map multimap (hash_set) (hash_multiset) (hash_map)(hash_multimap)

18 CSE 332: C++ STL containers Unique Associative Container Additional ExpressionsComplexity –Range constructor X a(i,j) linear –Insert element a.insert(t) logarithmic –Insert range a.insert(i,j) O(Nlog(size()+N)) setmap (hash_set) (hash_map)

19 CSE 332: C++ STL containers Multiple Associative Container Additional Expressions Complexity –Range constructor X a(i,j) linear –Insert element a.insert(t) logarithmic –Insert range a.insert(i,j) O(Nlog(size()+N)) multisetmultimap (hash_multiset) (hash_multimap)

20 CSE 332: C++ STL containers Sorted Associative Container Additional Expressions Complexity –Default constructors X (); X a; constant –Default constructor with comparator X a(c) constant –Range constructor X(i,j) O(NlogN) –Range constructor w/ comparator X a(i,j,c) O(NlogN) –Key comparison a.key_comp() constant –Value comparison a.value_comp() constant –Lower bound a.lower_bound(k) logarithmic –Upper bound a.upper_bound(k) logarithmic –Equal range a.equal_range(k) logarithmic –Insert with hint a.insert(p,t) logarithmic set multiset map multimap

21 CSE 332: C++ STL containers (Hashed Associative Container) Additional Expressions Complexity –Default constructors X (); X a; constant –Default constructor with bucket count X a(n) constant –Default constructor with hash function X a(n,h) constant –Default constructor with key equality X a(n,h,k) constant –Range constructor X a(i,j) linear –Range constructor with bucket count X a(i,j,n) linear –Range constructor w/ hash fxn X a(i,j,n,h) linear –Range constructor w/ key eq X a(i,j,n,h,k) linear –Hash function a.hash_funct() constant –Key equality a.key_eq() constant –Bucket count a.bucket_count() constant –Resize a.resize() linear (hash_set)(hash_multiset) (hash_map)(hash_multimap)


Download ppt "CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:"

Similar presentations


Ads by Google