Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010.

Similar presentations


Presentation on theme: "Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010."— Presentation transcript:

1 Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010 Standard Template Library (STL) By Eng. Yasmine Badr Adapted from www.tantalon.com/pete/embracingstl.ppt www.tantalon.com/pete/embracingstl.ppt

2 Spring 2010 Advanced Programming Section 1-STL Agenda Introduction Containers Iterators Algorithms Functors

3 Spring 2010 Advanced Programming Section 1-STL STL Standard C++ library of: – container classes, – algorithms, and – iterators; Provides many of the basic algorithms and data structures. A generic library, i.e. its components are heavily parameterized  almost every component in STL is a template.

4 Spring 2010 Advanced Programming Section 1-STL Advantages Standardized Thin & efficient Little inheritance; no virtual functions Small; easy to learn Flexible and extensible Naturally open source

5 Spring 2010 Advanced Programming Section 1-STL Containers Containers contain elements; they “own” the objects Containers provide iterators that point to its elements. Containers provide a minimal set of operations for manipulating elements

6 Spring 2010 Advanced Programming Section 1-STL Containers (cont.) Container DescriptionKeys vectordynamic array dequedynamic array -- both ends listlinked list setsorted list of keysno duplicate keys mapsorted list of key and value pairsno duplicate keys multisetsorted list of keysduplicate keys OK multimapsorted list of key and value pairsduplicate keys OK valarraysimilar to vector, where T is numeric, but optimized for numeric ops bitsetsimilar to vector, but fixed size and includes bit operators

7 Spring 2010 Advanced Programming Section 1-STL Vector Dynamic array, Contiguous block of memory reserve(), capacity() Insert invalidates all iterators if capacity changes Vectors good at: – Accessing individual elements by their position index (constant time). – Iterating over the elements in any order (linear time). – Add & remove elements from its end (const. time).

8 Spring 2010 Advanced Programming Section 1-STL Deque Double-ended queue Fast ins/erase at begin and end Insert invalidates all iterators Erase in middle invalidates all iterators Erase at begin/end invalidates iterators to begin/end

9 Spring 2010 Advanced Programming Section 1-STL List Doubly-linked list Fast insert/erase; no random access Special functions: splice(), merge() Erase invalidates iterators to erased elements; insert never invalidates any iterators

10 Spring 2010 Advanced Programming Section 1-STL Set List of sorted elements Fast retrieval based on key (log N) Fast insert/erase (log N) Red-black tree (balanced 2-3-4 tree) Erase invalidates erased elements Insert never invalidates any iterators

11 Spring 2010 Advanced Programming Section 1-STL Map Dictionary of sorted elements List of sorted key and value pairs

12 Spring 2010 Advanced Programming Section 1-STL Hash_Map & Hash_Set Not officially part of the STL standard, used to improve searching times. Implemented as a hash table, with each table entry containing a bidirectional linked list of elements. To ensure the fastest search times, make sure that the hashing algo. for the elements returns evenly distributed hash values.

13 Spring 2010 Advanced Programming Section 1-STL Container Adaptors Implemented by using the fundamental containers classes. Example adapator code stack > stackDeq; stackDeq.push(1); int i = stackDeq.top(); stackDeq.pop(); Adaptor Example containersDefault container stacklist, deque, vectordeque queuelist, dequedeque priority_queuelist, deque, vectorvector

14 Spring 2010 Advanced Programming Section 1-STL Iterators Generalization of pointers: they are objects that point to other objects Often used to iterate over a range of objects: Typical iteration c ::iterator i; for (i = c.begin(); i != c.end() ++i) // forward T t = *i; for (i = c.rbegin(); i != c.rend() ++i) // backward T t = *i;

15 Spring 2010 Advanced Programming Section 1-STL Algorithms Approx. 60 standard algorithms – searching (e.g. find()) – sorting (e.g. sort()) – mutating (e.g. transform()) Most functions take the form: – fn(c.begin(), c.end(),...)

16 Spring 2010 Advanced Programming Section 1-STL Algorithms (cont.) Examples: #include // return num elements equal to 123 int i = count(c.begin(), c.end(), 123); // negate all elements transform(c.begin(), c.end(),d.begin(), negate ()); // apply myFn to all elements for_each(c.begin(), c.end(), myFn); // shuffle the deck random_shuffle(deck.begin(), deck.end());

17 Spring 2010 Advanced Programming Section 1-STL Function Objects (Functors) C++ objects that can be called like a function to implement “callbacks” Use C++ operator()(...)

18 Spring 2010 Advanced Programming Section 1-STL Functors (cont.) Functors that do ordering are called “predicates” Example: struct StlStrPred // “public” class { bool operator()(const char* a, const char* b) { return (strcmp(a, b) == -1); } }; vector v; sort(v.begin(), v.end(), StlStrPred());

19 Spring 2010 Advanced Programming Section 1-STL STL Allocators Every STL container takes an allocator object as a template parameter template public AllocSpecial { public: pointer allocate(size_type, const void*); void deallocate(void*, size_type); //... other code here }; set setDefault; // default allocator // All Wensleydale allocations use special allocator set setSpecial;

20 Spring 2010 Advanced Programming Section 1-STL Vector Tips Use reserve() to set aside space when vector size is known in advance Trimming unused space v.swap(vector (v)); // vector, swap thyself!


Download ppt "Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010."

Similar presentations


Ads by Google