STL – Standard Template Library L. Grewe. 2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially.

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++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
CS 171: Introduction to Computer Science II Hashing and Priority Queues.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
Standard Containers: Vectors
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
More on the STL vector list stack queue priority_queue.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Standard Template Library (STL) Overview – Part 1 Yngvi Bjornsson.
Templates and the STL.
Writing Your Own STL Container Ray Lischner
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Spring 2010 Advanced Programming Section 1-STL Computer Engineering Department Faculty of Engineering Cairo University Advanced Programming Spring 2010.
Data Structures Using C++ 2E
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Containers Overview and Class Vector
111 © 2002, Cisco Systems, Inc. All rights reserved.
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)
C++ STL CSCI 3110.
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
Chapter 22 STL Containers §22.1 STL Basics §22.2 STL Iterators §22.3 Sequence Containers §22.4 Associative Containers §22.5 Container Adapters.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
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.
Data Structures for Midterm 2. C++ Data Structure Runtimes.
A gentle introduction to the standard template library (STL) Some references:
Intro to the C++ STL Timmie Smith September 6, 2001.
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
The Standard Template Library Container Classes Version 1.0.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
C++ Review STL CONTAINERS.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
More STL Container Classes ECE Last Time Templates –Functions –Classes template void swap_val (VariableType &a, VariableType &b) { VariableType.
CS212: Object Oriented Analysis and Design Lecture 26: STL Containers.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
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.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
Object-Oriented Programming (OOP) Lecture No. 41
Standard Template Library
Data Structures Interview Questions.
Standard Template Library (STL)
Collections Intro What is the STL? Templates, collections, & iterators
structures and their relationships." - Linus Torvalds
Associative Structures
Final Exam Review COP4530.
priority_queue<T>
CS212: Object Oriented Analysis and Design
Lecture 8 : Intro. to STL (Standard Template Library)
Collections Intro What is the STL? Templates, collections, & iterators
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Recitation Outline Hash tables in C++ STL Examples Recursive example
structures and their relationships." - Linus Torvalds
Standard Template Library
Presentation transcript:

STL – Standard Template Library L. Grewe

2 Goals Lots of important algorithms, data structures in CS using Templates. is a software library partially included in the C++ Standard Library. It provides containers (data structures), iterators, algorithms and functions.

3 STL data structures include: vector: Dynamic-array-backed –const-time random-access –const-time insert/delete at back –Log-time search if sorted list: doubly-linked list –fast insert/delete anywhere multiset: non-sequential, non-unique –Bag set: non-sequential, unique

4 Vectors vector nums; //notice the use of Templates!!!! vector vals(20); –init=-size-20 vector of doubles vector objs;

More (containers) data structures in STL vector a dynamic array, like C array (i.e., capable of random access) with the ability to automatically resize itself when inserting or erasing an object. Inserting and removing an element to/from back of the vector at the end takes amortized constant time. Inserting and erasing at the beginning or in the middle is linear in time. A specialization for type bool exists, which optimizes for space by storing bool values as bits.Carrayrandom accessamortizedbool list a doubly-linked list; elements are not stored in contiguous memory. Opposite performance from a vector. Slow lookup and access (linear time), but once a position has been found, quick insertion and deletion (constant time).doublylinked list dequedeque (double ended queue)queue a vector with insertion/erase at the beginning or end in amortized constant time, however lacking some guarantees on iterator validity after altering the deque. stack, priority_queue, queue 5 Ordered data structures

More (containers) data structures in STL set a sorted set; inserting/erasing elements in a set does not invalidate iterators pointing in the set. Provides set operations union, intersection, difference, symmetric difference and test of inclusion. Type of data must implement comparison operator < or custom comparator function must be specified. Implemented using a self-balancing binary search tree.setunionintersectiondifferencesymmetric differenceself-balancing binary search tree multiset same as a set, but allows duplicate elements. map a sorted associative array; allows mapping from one data item (a key) to another (a value). Type of key must implement comparison operator < or custom comparator function must be specified. Implemented using a self-balancing binary search tree.associative array multimap same as a map, but allows duplicate keys. hash_set hash_multiset hash_map hash_multimap similar to a set, multiset, map, or multimap, respectively, but implemented using a hash table; keys are not sorted, but a hash function must exist for key type. These containers are not part of the C++ Standard Library, but are included in SGI's STL extensions, and are included in common libraries such as the GNU C++ Library in the __gnu_cxx namespace. These are scheduled to be added to the C++ standard as part of TR1, with the slightly different names of unordered_set, unordered_multiset, unordered_map and unordered_multimap.hash tablehash functioncontainersTR1unordered_map 6 Un-Ordered data structures

7 STL class creation and population…vector example STL Template class similar to bag vector nums; nums.insert(8); nums.insert(1);... vector nums; nums.insert(8); nums.insert(1);...

8 STL Iterators Standard way to traverse through container: iteration Abstraction of both “index” and “pointer” –just: means of iterating –forward, back, etc.

9 The for-loop with the iterator Can also use array syntax –But it works with all STL containers for (vector ::iterator curs = nums.begin(); curs != nums.end(); ++curs) { cout << *curs; } for (vector ::iterator curs = nums.begin(); curs != nums.end(); ++curs) { cout << *curs; }

10 Sample of STL Algorithms void sort(begin it, end it) –it-s must be random-access –members must support ==, < void random_shuffle(begin it, end it) –same req’s bool binary_search(begin, end, target) –same req’s –also: assumes sorted min_element(v.begin(), v.end()) –returns iterator Work with all data structures (containers)!

11 STL summary Very useful/convenient in real life Implements many of the DSs we study here –Important to understand anyway! NOTE: STL containers are not intended to be used as base classes (their destructors are deliberately non-virtual). Deriving from a container is a common mistake made by novices. [1][2]containers container [1][2]