Standard Template Library vector, list, queue, stack, priority_queue, map, set, iterators, sort, stable_sort, permutations, copy, etc. STL & C++ 11 Telerik Algo Academy http://algoacademy.telerik.com Table of Contents 1. 2. Introduction to Standard Template Library Containers: Linear containers: list, vector, queue, stack Other containers: priority_queue, set/multiset, map/multimap 3. 4. Iterators Algorithms find, count, equal, copy, fill, swap_ranges sort and stable_sort make_heap, push_heap, pop_heap next_permutation and prev_permutation 2 Standard Template Library Introduction, Basic ADT Implementations STL Introduction Standard Template Library C++ Library Implements a lot of computer science fundamentals Container classes, Algorithms Iterators Mostly template-based Algorithms decoupled from containers through iterators 4 STL Introduction Containers Data structures, hold collections of elements Different benefits and downsides Implement fundamental Abstract Data Types Sequence, Associative, String, Adaptors… Iterators Provide access to container elements Used to "traverse" containers 5 STL Introduction Algorithms Fundamental algorithms over collections or single Hook up with iterators to access container elements Function objects Wrap functions/methods into objects Implement () operator – called like functions Called by algorithms and containers to act over elements (elements passed as parameters) 6 STL Introduction Live Demo STL Iterators Mechanism for traversing container elements STL Iterators "Smart" pointers to objects Specific for each container type Each container defines how it’s iterators work If we have an iterator to one element Increase/decrease it to get the other elements Types: Input, Output Forward iterator, Bidirectional iterator Random access iterator 9 Containers STL Container Architecture, Advanced Containers Containers STL Containers represent collections of objects Going through objects is called iterating Several base concepts & refinements for all STL containers: Container Forward Container, Reversible Container Random Access Container Concepts – definitions, not implementations Refinements – extensions/specific definitions Containers Containers also fall into one of two groups: Sequences Associative containers Actual data structures are models of the above i.e. implementations of the Concepts e.g. a vector is a model of a Sequence Containers – Pseudo-Diagram Container Forward, Reversible, Random Access Sequence Front/Back Insertion list Associative Container Simple, Pair, Sorted, Unique vector deque map set Adaptor priority_queue multiset multimap Containers STL Containers are templates A template is filled compile-time with type data i.e. a STL container is defined once compiled into different classes depending on its passed template parameters E.g. the container class vector is defined in the header <vector> once, but we can: use a vector<int>, to store integers use another vector<string> to store strings, etc. Containers Container concept Stores elements, element lifetime ends when container lifetime ends No guarantees on element order Forward Container concept (refinement of Container) Elements have some order Order won't change as a side effect of going through (iterating) the elements Guarantees "forward direction" of iteration Containers Reversible Container concept (refinement of Forward Container) Guarantees two (opposite) directions of iteration – "forward" and "backward" Random Access Container (refinement of Reversible Container) Guarantees (amortized) constant time access to any contained element i.e. can access an element, without iterating other elements to reach it Basic STL Containers Vector, List, Deque, Queue, Stack STL Vector Vector (#include <vector>) Defined: template <class T> vector Sequence, Random Access Stores a sequence of elements in contiguous memory Manages memory effectively Fast at retrieving elements by index and adding elements at the end Slow insertion/deletion in middle or beginning 18 STL Vector Declaring and initializing a vector #include<vector> //required header … vector<int> numbers; numbers.push_back(42); //numbers is now {42} numbers.push_back(13); //numbers is now {42, 13} int consoleNumber; cin>>consoleNumber; numbers.push_back(consoleNumber) Vector size and is obtained by calling vector<int> numbers; numbers.push_back(42); numbers.push_back(13); cout<<numbers.size(); //prints 2 size() STL Vector Accessing vector elements Done the same way as with arrays, i.e. [] vector<int> numbers; numbers.push_back(42); numbers.push_back(13); cout<<numbers[1]; //prints 13 cout<<endl; numbers[1] = numbers[0]; cout<<numbers[1]; //prints 42 Traversing a vector is the same as traversing an array (e.g. with a for loop) Element access does not depend on vector size STL vector Live Demo STL (Linked) List List (#include <list>) Defined: template <class T> list Sequence, Reversible Stores a sequence of elements in a doublylinked list Fast at deletion/insertion anywhere No random access to elements Have to traverse list to get to an item 22 STL (Linked) List Declaring and initializing a list list<int> numbers; numbers.push_back(2); numbers.push_front(1); numbers.push_back(3); List size and is obtained by calling size() List elements can be removed from front and back fast numbers.pop_front(); numbers.pop_back(); STL (Linked) List Accessing list elements front and back methods provide first and last element access cout<<numbers.front(); cout<<numbers.back(); Only way to get access to all elements – traversal by iterator list<int>::iterator numbersIterator; for(numbersIterator = numbers.begin(); numbersIterator != numbers.end(); numbersIterator++) { cout<<*numbersIterator<<endl; } STL list Live Demo STL Queue Queue (#include<queue>) Defined: template <class T> queue Sequence Adaptor First in, First out structure (FIFO) Stores a sequence of elements Provides access only to first element Can remove only at front Can add only at back 26 STL Queue Declaring and initializing a queue queue<int> q; q.push(1); q.push(2); q.push(3); Queue size is obtained by calling Queues allow size() removing elements only from the front of the sequence q.pop(); STL Queue Accessing queue elements front and back methods provide first and last element access cout<<q.front(); cout<<q.back(); Other types of access to queue elements are meaningless The idea of the queue is to restrict access and be FIFO STL queue Live Demo STL Stack Stack (#include <stack>) Defined: template <class T> stack Sequence adaptor Last in, First out structure (LIFO) Stores a sequence of elements Provides access only to last element Can remove or add elements only at back/top 30 STL Stack Declaring and initializing a stack stack<int> s; s.push(1); s.push(2); s.push(3); Stack size is obtained by calling size() Stacks allow removing elements only from the back (top) of the sequence s.pop(); STL Stack Accessing stack elements top method provides first element access cout<<s.top(); Other types of access to stack elements are meaningless The idea of the stack is to restrict access and be LIFO STL stack Live Demo Advanced Container Models priority_queue, map, set, multimap, multiset, Usage and Examples Priority Queue The priority_queue is a queue Enables insertion of elements Enables access/removal of "top" element "First" element is referred to as "top" element Guarantees the top element is the largest Biggest for numbers (by default) Last lexicographically for strings (by default) Or according to a comparer Or overloaded operators for other types Priority Queue Priority Queue (#include <queue>) priority_queue<T, Sequence = vector<T>, Compare = less<T> > T has to be able to be compared by Compare Fast at accessing the top element (1) Fast at inserting elements (log n) Good at removing top element (log n) Uses a container for storing elements (default: vector) 36 Priority Queue Declaring and initializing a priority queue: #include<queue> //required header … priority_queue<int> numsBySize; numsBySize.push(1); numsBySize.push(3); numsBySize.push(2); priority_queue<string> stringsByLex; stringsByLex.push("a"); stringsByLex.push("c"); stringsByLex.push("b"); Retrieving top element: numsBySize.top() //returns 3, does not remove it Removing top element: stringsByLex.pop() //removes "c" Priority Queue Usage Live Demo Problems Solved with Priority Queues Finding shortest path in weighted graphs (Dijkstra's algorithm uses priority queues) Getting largest N items from several sorted lists Compression in Huffman coding Heapsort (STL priority queue uses a heap) Simple problem: Given a sequence of numbers, each time a numbers is 0, print the largest number so-far Simple Problem Solved with a Priority Queue Live Demo Associative Container Models Several categories Simple, Sorted, Unique Simple Associative Containers Elements are their own keys Pair Associative Container Values are in the form (key, element) Sorted Associative Containers Elements ordered, most operations are log n Unique Associative Containers No duplicate keys are allowed Set Categories: Sorted, Simple, Unique Elements are their own keys E.g. if you want to check if an element is contained, you query with the (copy) of the element Guarantees no duplicate elements Extracting elements one by one: Yields the elements in ascending order Set Set (#include <set>) set<Key, Compare = less<Key>, Alloc = new> Key has to be comparable by Compare Fast checking if an element is contained (log n) Fast inserting elements (log n) Fast deleting (erasing) elements (log n) Deleting elements does not invalidate iterators to other elements 43 Set Declaring and initializing a set: #include<set> //required header … set<int> uniqueNums; uniqueNums.insert(3); uniqueNums.insert(7); uniqueNums.insert(2); uniqueNums.insert(7); Set elements can be accessed through iterator (and consequently iterated in ascending order) uniqueNumbers.begin(); //iterator to first element (2) Set elements can be removed By iterator By value uniqueNums.erase(uniqueNums.begin()); uniqueNums.erase(2); Set Usage Live Demo Problems Solved with Sets Any problems including mathematical set operations Unions, intersections, etc. Set Cover Problem Simple problem: You are given a sequence of numbers. Print all numbers in the sequence, without printing the same number more than once Simple Problem Solved with a Set Live Demo Multiset Same as a set, without the Unique category i.e. there can be repeating elements All other operations & properties are the same Multiset (#include <set>) multiset (same template parameters as set) Declaring and initializing a multiset #include<set> //required header … multiset<int> nums; nums.insert(2); nums.insert(2); //nums contains: 2, 2 Multiset Usage Live Demo Map Categories: Sorted, Unique, Pair Each element has a key Accessing elements is done through the key Guarantees no duplicate elements Element keys are iterated in increasing Often pictured as an array, order the indices of which can be any type – number, string or even some class Map Map (#include <map>) map <Key, Data, Compare = less<Key> Alloc = new> Key must be comparable by Compare Fast at accessing elements by key (log n) Fast at deleting elements by key (log n) Fast at inserting elements by key (log n) Values are of the type std::pair<Key, Data> Iterators will point to std::pair objects Map Declaring and initializing a map: #include<map> //required header … map<char*, int> peopleAges; peopleAges["Joro"] = 22; peopleAges.insert(pair<char*, int>("Petya", 20)); Accessing element by key: peopleAges["Petya"]; peopleAges["Petya"]++; Removing element by key: peopleAges.erase("Joro"); Map Usage Live Demo Problems Solved with Maps Maps have similar efficiency as hash-tables, but keep elements ordered Many compression algorithms use maps/hash- tables Several cryptographic attacks use maps/hash- tables Simple Problem: You are given a sentence of words. Count how many times each word occurs in the text. Simple Problem Solved with a Map Live Demo Multimap Same as a map, without the Unique category i.e. there can be repeating elements No [] operator, as there can be multiple values, corresponding to the same key All other operations & properties are the same Most element access is done through iterators Multimap Multimap (#include <map>) multimap (same template parameters as map) Declaring and initializing a multimap #include<map> //required header … multimap<string, string> personNicks; personNicks.insert(pair<string, string>("George", personNicks.insert(pair<string, string>("George", personNicks.insert(pair<string, string>("George", personNicks.insert(pair<string, string>("George", "Joro")); "Gosho")); "Joro")); "Gopi")); Multimap Usage Live Demo Iterators The Way of STL Element Access Iterators Iterators are a pattern in STL Enable access to container elements For almost any container's elements Most container iterators have similar mechanics to pointers Can be incremented, to point to next element Can be dereferenced, to get element value Can use -> operator to access element members Can be const Iterators Each container defines its own iterator type A class inside the container's class Syntax to access iterator type container_type<template_parameters...>::iterator Example for an iterator of a vector<int>: vector<int> numbers; numbers.push_back(1); numbers.push_back(2); vector<int>::iterator numsIter = numbers.begin(); cout<<*numsIter<<endl; numsIter++; cout<<*numsIter<<endl; Iterators: Simple Example Live Demo Iterators and Containers Syntax, Usage, Examples Iterators Several types of iterator Concepts with differing purposes and functionality Output Iterator Supports storing values i.e. writing values to the pointed element i.e. mutable Supports incrementing Other operations are not necessarily supported i.e. no guarantee on dereferencing, comparing… Iterators Input Iterator Supports dereferencing Supports incrementing Does not necessarily support storing values i.e. writing values to the pointed element i.e. not necessarily mutable Opposite of Output iterator, in some sense Iterators Forward Iterator Refinement of Input & Output iterators Reflects the idea of a linear sequence of values Allows multipass algorithms Implementations can be mutable/immutable Can only increment, i.e. can't "go back", only "forward" Iterators Bidirectional Iterator Refinement of Forward Iterator Can be both incremented and decremented i.e. allows "going back" Iterators Random Access Iterator Refinement of Bidirectional Iterator Constant-time moving in arbitrarily-sized steps i.e. can increment/decrement with any value, not just 1 step like other iterators e.g. increment a random access iterator 5 steps: vector<int>::iterator iter = someVector.begin(); iter+=5; Note: using vector iterator, as it is a Random Access Iterator Iterators and Containers Most container operations require iterators or return iterators erase(), find(), insert() to name a few Some containers require iterators To access elements in a meaningful way list, set, multiset, multimap Iterating over maps/sets Gives the elements in order Note: Container iterators are at least Forward Iterators and Containers Iterating a container i.e. go through container elements with iterator Instantiate an iterator of the container's type Set it to the beginning (usually Start .begin()) a loop, incrementing the iterator Stop if the iterator equals .end() of container end() – iterator pointing "after the last element" At each step, the iterator will point to an element (unless you've reached end()) Iterators and Containers Iterating a set<string> Result: strings in lexicographical order set<string> orderedStrings; orderedStrings.insert(...); ... for(set<string>::iterator iter = orderedStrings.begin(); stringsIter != orderedStrings.end(); stringsIter++) { cout<<(*stringsIter)<<endl; } Iterating Over Containers Live Demo Iterators and Containers Don't overuse iterators Especially for Random Access Containers vector and deque support the [] operator For both, [] does fast pointer arithmetic Faster to traverse by index, than by iterator Iterators and Containers Iterators are the standard link between data structures and algorithms in STL Some algorithms will return results by iterator You will have to iterate the results The power of iterators: Separate containers from algorithms Any algorithm can work on any container But they don't need to know about each other As long as both understand iterators Iterators and Containers Simple problem: You are given a sentence and a word contained in that sentence. Show the positions at which the word is (first word is position 0, second position 1, etc.) Common STL Algorithms Sorting, Searching, Mutating, Heaps, Combinatorics, etc. Common STL Algorithms The STL has a wide range of built-in algorithms, in several categories Non-mutating (searching, counting, etc.) Mutating (removing, rotating, swapping, etc.) Sorting Heap operations (make heap, push heap, etc.) Combinatorial (next/previous permutation) Set, comparison, numeric, min/max operations Common STL Algorithms Some common principles used by algorithms Algorithms on ranges take them as iterators In the form [first, last), i.e. last is non-inclusive Inserting a new element at an iterator Pushes any existing element at that iterator forward (i.e. to the next iterator position) Searching, Counting, Matching find(first, last, value) Returns 1st iterator, pointing to same value If no such iterator exists – returns last count(first, last, value) Returns the number of elements, equal to value equal(first1, last1, first2) Checks if the range [first1, last) is element-by-element equal to the range [ first2, first2 + (last1 – first1) ) Searching, Counting, Matching Live Demo Mutating Algorithms copy(first, last, result) Copies the Input Iterator range [first, last) Into the Output Iterator result fill(first, last, value) Sets all elements in the range to value swap_ranges(first, last, first2) Exchanges the range [first1, last) with the range [first2, first2 + (last1 – first1)) and returns the element after the second range Mutating Algorithms Live Demo Sorting Algorithms sort(first, last) Sorts the Random Access Iterator range in ascending order stable_sort(first, last) Same as sort, but keeps relative ordering of equal elementsas Potentially a bit slower than sort in its worst case Sorting Algorithms Live Demo Heap Operations Note: STL Heaps are what priority_queue uses. The first/top element is largest, it has 2 child elements which are smaller, they are first/top elements of their own heaps, etc. All STL Heap operations use Random Access Iterators make_heap(first, last) Makes the range into a heap push_heap(first, last) Assumes [first, last – 1) is a heap The element at last – 1 is placed in the heap pop_heap(first, last) Assumes [first, last) is a heap, removes top Heap Operations, a.k.a. How to Make a Priority Queue Live Demo Combinatorial Algorithms next_permutation(first, last) Transforms the Bidirectional Iterator range into the lexicographically next permutation of the elements. If the last permutation has already been reached, transforms into the first permutation (i.e. sorts ascending) prev_permutation(first, last) Same as next_permutation, but transforms into the previous permutation Permutation Algorithms Live Demo Standard Template Library курсове и уроци по програмиране, уеб дизайн – безплатно курсове и уроци по програмиране – Телерик академия уроци по програмиране и уеб дизайн за ученици програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки курсове и уроци по програмиране, книги – безплатно от Наков уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop free C# book, безплатна книга C#, книга Java, книга C# безплатен курс "Качествен програмен код" безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge форум програмиране, форум уеб дизайн ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET ASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC алго академия – състезателно програмиране, състезания курс мобилни приложения с iPhone, Android, WP7, PhoneGap Дончо Минков - сайт за програмиране Николай Костов - блог за програмиране C# курс, програмиране, безплатно http://algoacademy.telerik.com