Standard Template Library

Similar documents
Template based set of collection classes STL collection types (container types)

SSE2034: System Software Experiment 3

STL: C++ Standard Library

CS183 Software Design. Textbooks. Grading Criteria. CS183-Su02-Lecture 1 20 May by Eric A. Durant, Ph.D. 1

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Dynamic Data Structures

CS 103 Unit 12 Slides

Abstract Data Types 1

PIC 10A. Lecture 23: Intro to STL containers

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Abstract Data Types 1

To know the relationships among containers, iterators, and algorithms ( 22.2).

The Standard Template Library Classes

Queue Implementations

Chapter 17: Linked Lists

CS197c: Programming in C++

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Lectures 11,12. Online documentation & links

Sequence Containers. Cristian Cibils

2014 Spring CS140 Final Exam - James Plank

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

Chapter 17: Linked Lists

EE 355 Unit 10. C++ STL - Vectors and Deques. Mark Redekopp

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

STL Standard Template Library

Containers: Queue and List. Jordi Cortadella and Jordi Petit Department of Computer Science

Programming in C++ using STL. Rex Jaeschke

EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp

19.1 The Standard Template Library

Lecture 21 Standard Template Library. A simple, but very limited, view of STL is the generality that using template functions provides.

Lectures 19, 20, 21. two valid iterators in [first, last) such that i precedes j, then *j is not less than *i.

Teaching with the STL

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

STL. Prof Tejada Week 14

Chapter 18: Stacks And Queues

CS302. Today s Topics: More on constructor/destructor functions More on STL <list>

The Standard Template Library. An introduction

1 Short Answer (7 Points Each)

Unit 4 Basic Collections

Chapter 5. The Standard Template Library.

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation

/ binary_search example #include <iostream> // std::cout #include <algorithm> // std::binary_search, std::sort #include <vector> // std::vector

by Pearson Education, Inc. All Rights Reserved. 2

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

A linear structure is an ordered (e.g., sequenced) arrangement of elements.

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

Sequential Containers. Ali Malik

VI. Templates A. Introduction.

CPSC 427a: Object-Oriented Programming

Standard Library Reference

1. The term STL stands for?

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Sequential Containers. Ali Malik

7 TEMPLATES AND STL. 7.1 Function Templates

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Advanced C++ STL. Tony Wong

Templates and Vectors

Object-Oriented Programming for Scientific Computing

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Associative Containers and Iterators. Ali Malik

Simulations. buffer of fixed capacity using the STL deque. modeling arrival times generating and processing jobs

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

Stacks and their Applications

Chapter 18: Stacks And Queues

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

C++ Standard Template Library. Contents. Pierre Fierz. 1 Introduction. 2 Organization of STL Header Files. Organization of STL Header Files

Container Notes. Di erent Kinds of Containers. Types Defined by Containers. C++11 Container Notes C++11

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil

List, Stack, and Queues

Doubly-Linked Lists

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

CS 103 Unit 11. Linked Lists. Mark Redekopp

COEN244: Class & function templates

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Name: 1 stack<int> s; 2 s.push(9) 3 s.push(5) 4 s.push(10) 5 s.push(1) 6 s.pop() 7 s.pop() 8 s.push(0) 9 s.pop() 10 s.pop() 11 s.

CSC 222: Computer Programming II. Spring 2004

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

MODULE 33 --THE STL-- ALGORITHM PART I

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

Standard Template Library. Outline

CS11 Advanced C++ Spring 2018 Lecture 2

LECTURE 03 LINKED LIST

G Programming Languages - Fall 2012

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination

CS11 Advanced C++ Fall Lecture 1

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists

CISC 3130 Data Structures Spring 2018

Polymorphism. Programming in C++ A problem of reuse. Swapping arguments. Session 4 - Genericity, Containers. Code that works for many types.

Iterators. node UML diagram implementing a double linked list the need for a deep copy. nested classes for iterator function objects

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Standard Template Library

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Transcription:

Standard Template Library Wednesday, October 10, 2007 10:09 AM 9.3 "Quick Peek" STL history 1990s Alex Stepanov & Meng Lee of HP Labs 1994 ANSI/IS0 standard Components Container class templates Iterators Algorithm templates Iterators are interface between containers & algorithms Examples vector - dynamic array deque - double-ended queue stack queue list 9.7 deque, stack, queue Deque operations constructor push_front (elem) push_back (elem) front() back() pop_front() pop_back() Deque example deque<int> d; d.push_front(3); d.back() = 5; d.pop_front(); Deque Notes allows [] operator allows insert & delete at any point like vectors iterators act like vector iterators memory organized as series of memory blocks, typically 4KB Example: push_front 555 push_back 1, 2, 3, 4 push_front 77 map 0 Block 2 1 Block 1 Block 1 contains 555, 1, 2, 3, 4 CS223 Page 1

Block 2 contains 77 Stack operations constructor - wraps around a container STL top() push(elem) pop() size() comparison operators Stack example stack<int, vector<int>> istack; stack<int> bstack; uses deque as container Queue operations constructor - also wraps around container front() back() pop() push(elem) Queue example queue<int, vector<int>> aqueue; queue<int> iqueue; use deque as container 10.5 Standard Algorithms operate on container iterators sort using < to compare elements vecto<int> v; // put stuff in v sort(v.begin(), v.end()); using "less-then" to compare (a function) bool LessThan(int a, int b) { return a > b; } int main() { vector<int> v; // add to v sort(v.begin(), v.end(), LessThan); } Other STL algorithms binary_search (begin, end, value) find (begin, end, value) search (begin1, end1, begin2, end2) search for a sequence of values copy (begin, end, container) count (begin, end, value) how many times value occurs sort (begin, end) unique (begin, end) reverse (begin, end) more algorithms listed in book pp 570-2 11.3 STL list variation on doubly linked list CS223 Page 2

comparison to other STL Containers does not allow [] operator like deque cannot use STL algs like sort() good at inserting & deleting at any point good for sequential iteration higher overhead then deque iterator is bidirectional only (no random access) supports following operators: ++ to go to next node -- to go to previous node * to access data in current node = to assign one iterator to another == and!= to compare two iterators declaring iterator: list<int>::iterator i; list<int>::const_iterator ic; // read only // access to list elements list<int> ld; //add data to ld i= ld.begin(); // head node i++; // second node i--; // back to head node operations constructors default creates empty list list(int n) creates list w/ n slots list(int n, T value) creates list w/ n slots that all have passed value list(startptr, endptr) creates list w/ contents of startptr up to (not including) endptr copy constructor destructor size() push_back(t elem) tail insert push_front(t elem) head insert insert(position, T elem) position is an iterator returns iterator to new node insert(position, int n, T elem) put n copies of element at given position insert (position, startptr, endptr) does not include endptr, like constructor pop_back() tail delete pop_front() head delete erase(position) delete node at position erase(position1, position2) delete from position1 to position 2 remove(t elem) delete all nodes containing elem unique() collapse repeating sequences front() retrieve head's value back() retrieve tail's value begin() return iterator to head end() return iterator to 1 past tail rbegin() return reverse iterator to tail CS223 Page 3

rbegin() return reverse iterator to tail rend() return reverse iterator to 1 before head sort() sort using < operator reverse() reverse order of elements merge(list2) place elements from list2 into this list in < sorted order, remove all elements from list2. both lists must first be sorted. splice(position, list2) place elements from list2 into this list in list2 order at the given position. remove elements from list2 splice(to-pos, list2, from-pos) start in list2 at iterator from-pos instead of whole list2 splice (position, list2, start, end) take elements from start to end (not including end) from list2 swap(list2) swap this list w/ list2 list1= list2 list1 == list2 elements must be in same order for both lists list1 < list2 lexicographical less than Demo of list code // must define output operator template <class T> ostream & operator << (ostream & o, const list<t> & l) { list<t>::const_iterator i; for(i = l.begin(); i!= l.end(); i++) o << *i << " "; return o; } int main() { list<int> la; // default constructor list<int> lb(3); // set slots list<int> lc(5, 11); // set slots & default val int array[] = {2, 22, 222, 2222}; list<int> ld(array, array+4); list<int>::iterator i; i = lc.begin(); lc.insert(i, 65); lc.insert(i, 3, 78); // 3 copies lc.insert(i, array, array + 4); cout << lc << endl; i= find(lc.begin(), lc.end(), 65); if (i == lc.end()) cout << "value 65 not found in list\n"; else cout << "Value 65 found\n"; lc.remove(22); i = lc.end(); i--; i--; CS223 Page 4

lc.erase(lc. begin(), i); more examples in book pp 608-616 How list works uses a doubly linked circular list w/ a dummy (never used for data) head node keeps stack of free nodes instead of using new & delete all of the time -only allocates when stack is empty -one stack for each datatype -allocates a chunk of memory & breaks into nodes for a free stack -deallocates stack for datatype T only when all lists for T have been deleted iterators begin() points to 1st actual node, skips dummy head node end() points to dummy head node rbegin() points to tail rend() points to dummy head node CS223 Page 5