Standard Template Library

Slides:



Advertisements
Similar presentations
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Advertisements

. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
Writing Your Own STL Container Ray Lischner
Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010.
Data Structures Using C++ 2E
Containers Overview and Class Vector
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Generic Programming Using the C++ Standard Template Library.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
Data Structures Using C++ 2E Chapter 13 Standard Template Library (STL) II.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
Lecture 7 : Intro. to STL (Standard Template Library)
Computer Science and Software Engineering University of Wisconsin - Platteville 11.Standard Template Library Yan Shi CS/SE 2630 Lecture Notes.
CS 403: Programming Languages Lecture 24 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Data Structures for Midterm 2. C++ Data Structure Runtimes.
CS212: Object Oriented Analysis and Design Lecture 28: Functors.
Intro to the C++ STL Timmie Smith September 6, 2001.
STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially.
The Standard Template Library Container Classes Version 1.0.
C++ Review STL CONTAINERS.
More STL Container Classes ECE Last Time Templates –Functions –Classes template void swap_val (VariableType &a, VariableType &b) { VariableType.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
STL Associative Containers navigating by key. Pair Class aggregates values of two, possibly different, types used in associative containers defined in.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
Final Exam Review COP4530.
CS212: Object Oriented Analysis and Design
Regarding homework 9 Many low grades
Standard Template Library
Standard Template Library
Exceptions, Templates, and the Standard Template Library (STL)
STL – Standard Template Library
Standard Template Library (STL)
ENERGY 211 / CME 211 Lecture 19 November 3, 2008.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Starting Out with C++ Early Objects Eighth Edition
What remains Topics Assignments Final exam
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Programming with Concepts
18 – Sequential Containers
Chapter 22: Standard Template Library (STL)
Miscellaneous Stuff Which there wasn’t enough time to cover
CS212: Object Oriented Analysis and Design
Final Exam Review COP4530.
Generic Programming Karl Lieberherr 12/1/2018 Generic Programming.
priority_queue<T>
The Standard Template Library
Elements are always copied when they are put into a container
C++ STL, Smart Pointers Intro CSE 333 Spring 2018
Standard Version of Starting Out with C++, 4th Edition
Exceptions, Templates, and the Standard Template Library (STL)
Lecture 8 : Intro. to STL (Standard Template Library)
Containers: Queue and List
C++ Standard Template Library CSE 333 Summer 2018
Standard Template Library
Standard Template Library
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Presentation transcript:

Standard Template Library Jim Fawcett Summer 2017 Standard Template Library

Standard Template Library Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map and unordered_multimap are standard associative containers. Iterators: Input iterators are read only – each iterated element may be read only once. Output iterators are write-only – each iterated element may be written only once. Forward iterators can read or write an element repeatedly. They don’t support operator--() so they can only move forward. Bidirectional iterators are like forward iterators except that they support moving in both directions with operator++() and operator--(). Random access iterators are bidirectional iterators that add the capability to do iterator arithmetic – that is they support *(it + n); Any class that overloads the function call operator - operator() - is a functor class, and we refer to its instances as functors or function objects. Standard Template Library

STL Supports Guaranteed Complexity for Container Operations Vectors and Deques: Insertion is a linear time operation. Accessing a known location is constant time. Searching an unsorted vector or deque is a linear time operation. Searching a sorted vector or deque should be a logarithmic time operation ( use binary_search algorithm to ensure that it is). Lists: Insertion is a constant time operation. Accessing a known location and searching, whether sorted or not, is linear time, with the exception of the end points, which can be accessed in constant time. Sets and Maps: Based on Red-Black binary tree. Insertion and accessing are logarithmic time operations. Searching should be a logarithmic time operation (use member function find, etc., to ensure that it is). Standard Template Library

STL Supports Guaranteed Complexity for Container Operations Unordered_set and Unordered_map Based on hashtable Lookup, insertion, and deletion are (nearly) constant time operations They are hashed containers, so we get access to an element by computing a hash function on a key which maps to an address in the table. This is constant time. If there is more than one element that hashes to that address then we search a linked list rooted at that address (the elements on this list are referred to as a bucket). So access is nearly constant time. Standard Template Library

STL Header Files for Containers Include Container Type Description <array> array<T> Fixed array of elements <deque> deque<T> Double ended queue, fast insert/remove from either end, indexable. <list> list<T> Doubly-linked list, fast insert/erase at current location and either end, slow traversal <map> map<key, value> mulitmap<key, value> Associates values with sorted list of keys, fast insert/remove, fast access with index, fast binary search, is indexable with keys. <queue> queue<T> priority_queue<T> First in, first out queue, efficient push and pop. Efficient removal of largest. <set> set<T> multiset<T> Set of sorted keys, fast find/insert/removal. <stack> stack<T> Last in, first out stack. <unordered_map> unordered_map<K,V> Unordered_multimap<K,V> Unordered key/value collection, constant time lookup, insertion, removal. <unordered_set> unordered_set<V> unordered_multiset<V> Unordered collection of values, constant time lookup, insertion, removal. <vector> vector<T> Slow insert, delete, except at the end, fast access with index. Slow find. Capacity grows as needed. Standard Template Library

Standard Template Library Other STL Header Files Include Types Description <algorithm> Find, find_if, search, copy, fill, count, generate, min, sort, swap, transform, … Applied to a container over an iterator range. <functional> Function, bind, divides, equal_to, greater, less, negate, minus, plus, … Function objects passed to an algorithm to operate on elements of a container. <iterator> operator+, operator=, operator++, operator--, operator*, operator->, … Defines current location, range of action on a container or stream. <memory> unique_ptr, shared_ptr, allocator, operator==, operator!=, operator=, operator delete, operator new, … Provides smart pointer for managing resources on native heap, supports redefinition of allocation policy for containers. <numeric> Accumulate, product, partial sum, adjacent difference, … Applied to a container over an iteration range. <utility> Pair, operator!=, operator<=, operator>=, operator>, … Pair struct and global operators. Standard Template Library

Standard Template Library STL Iterators Input iterator Read only, move forward Example: istream_iterator Output iterator Write only, move forward Examples: ostream_iterator, inserter, front_inserter, back_inserter Forward iterator Read and write, move forward Example: forward_list<T>::iterator Bidirectional iterator Read and write, move forward and backward Examples: list<T>::iterator, map<K,V>::iterator Random access iterator Read and write, random access Examples: vector<T>::iterator, deque<T>::iterator, C++ pointers Standard Template Library

Standard Template Library STL Functions unary functions: Function taking single template argument Will be instantiated with container’s value_type // unary function template <typename T> void printElem(T val) { cout << “value is: “ << val << endl; } void main( ) { list< int > li; : // unary function used in algorithm for_each(li.begin(), li.end(), printElem); } for_each calls printElem with values from list Standard Template Library

Standard Template Library STL Functions predicate: function taking a template type and returning bool // predicate template <class T> bool ispositive(T val) { return (val > 0); } void main( ) { list<int> li; : // return location of first positive value list<int>::iterator iterFound = find_if(li.begin(), li.end(), ispositive<int>); } Standard Template Library

Standard Template Library STL Function Objects Function objects: class with constructor and single member operator() template <class T> class myFunc { public: myFunc( /*arguments save needed state info */) { } T operator()(/* args for func obj */) { /* call some useful function with saved state info and args as its parameters */ } private: /* state info here */ } Standard Template Library

Standard Template Library std::function adapted from example in https://oopscenities.net/2012/02/24/c11-stdfunction-and-stdbind/ #include <functional> #include <iostream> #include <string> #include <vector> void execute(const std::vector<std::function<void()>>& fs) { for (auto& f : fs) f(); } void plain_old_func() std::cout << "\n I'm a plain old function"; class functor public: void operator()() const std::cout << "\n I'm a functor"; }; int main() { std::vector<std::function<void()>> x; x.push_back(plain_old_func); functor functor_instance; x.push_back(functor_instance); x.push_back([]() std::cout << "\n Hi, I'm a lambda expression"; }); execute(x); std::cout << "\n\n"; } Standard Template Library

Standard Template Library std::bind adapted from example in https://oopscenities.net/2012/02/24/c11-stdfunction-and-stdbind/ #include <functional> #include <iostream> #include <string> #include <vector> void execute(const std::vector<std::function<void()>>& fs) { for (auto& f : fs) f(); } void show_text(const std::string& t) std::cout << "\n Text: " << t; int main() std::vector<std::function<void()>> x; x.push_back(std::bind(show_text, "Bound function")); execute(x); std::cout << "\n\n"; Standard Template Library

Standard Template Library STL Function Objects Standard Template Library

Standard Template Library Algorithms by Type Standard Template Library

Algorithms by Type (continued) Standard Template Library

Standard Template Library End of Presentation Standard Template Library