PIC 10A. Lecture 23: Intro to STL containers

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

STL Standard Template Library

STL: C++ Standard Library

Standard Template Library

CS197c: Programming in C++

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

Dynamic Data Structures

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

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

List, Stack, and Queues

Computer Science II CSci 1200 Lecture 8 Iterators; Programming Examples

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

1. The term STL stands for?

Exceptions, Templates, and the STL

CS11 Advanced C++ Fall Lecture 1

CSE 100: C++ TEMPLATES AND ITERATORS

SSE2034: System Software Experiment 3

Standard Library. Lecture 27. Containers. STL Containers. Standard Library

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

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

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

Chapter 5. The Standard Template Library.

CSI33 Data Structures

Chapter 4 Computation

Abstract Data Types 1

CSE100. Advanced Data Structures. Lecture 4. (Based on Paul Kube course materials)

Chapter 4. Computation. Bjarne Stroustrup.

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

CSE 100: C++ TEMPLATES AND ITERATORS

Abstract Data Types 1

Standard Template Library

Computational Physics

The Standard Template Library Classes

7 TEMPLATES AND STL. 7.1 Function Templates

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

Lecture-5. STL Containers & Iterators

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists!

CPSC 427a: Object-Oriented Programming

CS 215 Lecture 17 Linked lists and time complexities

Section 1: True / False (2 points each, 30 pts total)

Standard Template Library. Containers, Iterators, Algorithms. Sequence Containers. Containers

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

the Stack stack ADT using the STL stack are parentheses balanced? algorithm uses a stack adapting the STL vector class adapting the STL list class

CSCI-1200 Computer Science II Spring 2006 Test 3 Practice Problem Solutions

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

Unit 4: Stacks and Queues

MODULE 33 --THE STL-- ALGORITHM PART I

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

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

Computer Programming

CS 32. Lecture 5: Templates

CSE 100: C++ TEMPLATES AND ITERATORS

C++ 11 and the Standard Library: Containers, Iterators, Algorithms

Data Structures and Algorithms

Containers in C++ and Java

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Sequential Containers. Ali Malik

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

1 P age DS & OOPS / UNIT II

Sequential Containers. Ali Malik

Module 9. Templates & STL

Lectures 11,12. Online documentation & links

Programming Abstraction in C++

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

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

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

Sequence Containers. Cristian Cibils

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

(8 1) Container Classes & Class Templates D & D Chapter 18. Instructor - Andrew S. O Fallon CptS 122 (October 8, 2018) Washington State University

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists

Associative Containers and Iterators. Ali Malik

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

CSCI-1200 Data Structures Fall 2017 Lecture 17 Trees, Part I

Name CPTR246 Spring '17 (100 total points) Exam 3

the Queue queue ADT using the STL queue designing the simulation simulation with STL queue using STL list as queue using STL vector as queue

The Standard Template Library. An introduction

Container Class and Integrators, Proxy Class EC6301-OOPS AND DATA STRUCTURES

Introduction. Programming in C++ Pointers and arrays. Pointers in C and C++ Session 5 - Pointers and Arrays Iterators

Templates and Vectors

Programming Abstractions

Standard Template Library STL

Lab 4X - Intro to MOOS Programming HINTS

Stacks and queues (chapters 6.6, 15.1, 15.5)

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

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

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

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

CSCI-1200 Data Structures Spring 2015 Lecture 2 STL Strings & Vectors

COEN244: Class & function templates

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

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

Computer Science II Lecture 2 Strings, Vectors and Recursion

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

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

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

CSCI-1200 Data Structures Spring 2015 Lecture 18 Trees, Part I

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

Tree Travsersals and BST Iterators

Transcription:

PIC 10A Lecture 23: Intro to STL containers

STL STL stands for Standard Template Library There are many data structures that share similar features These data structures can be manipulated by a common set of algorithms STL is the C++ library that contains these data structures and algorithms You already know one STL container!!

Different types of STL containers Fundamental containers (sequential containers) Vector List Deque Adapters (Built using the fundamental containers) Queue Stack Priority queue Associative containers (elements ordered for fast retrieval) Set Map

Linked lists In a linked list objects are strung together by "arrows" Data Next Previous Data Next Previous Data Next Previous Every object has an "arrow" to a previous and next objects.

Lists Advantages Speedy insertion and deletion compared to vectors Disadvantage No indexing

Example list<int> L; L.push_back(3); L.push_back(5); L.push_front(0); L.push_back(6); What does L look like?

Iterators How can we access elements in a data structure that has no indexes? Some data structures elements are not even ordered in any particular way. All STL containers have iterators that can be used to step through the container. Iterator is an object that is storing a location of an element in the container and it can be incremented to find the next iterator. See example for lists.

Lists list<int>::iterator i; // Declare an iterator i = L.begin(); // Set the iterator to the beginning of L ++i; // With iterators it is better to get into the habit of using pre increment. ++i; L.insert(i,2); For( i = L.begin(); i!= L.end(); ++i ) cout << *i << " "; cout << endl;

Iterators Iterators can be used to step through containers: for( i = L.begin(); i!= L.end(); ++i ) cout << *i << " "; cout << endl; *i is used to access the object that i is associated with

Common mistake list<int> one; list<int> two; //both lists are filled list<int>::iterator p = one.begin(); while(p!= two.end()) { } //stuff ++p;

Iterator ranges Note that for all the containers c.end() Is an iterator, but it is actually out of bounds. It is the first iterator that is out of bounds and it should not be accessed using the *operator. When we think of two iterators representing a range the first iterator corresponds to an object at the beginning of the range, however, The second iterator only marks the end object and is not considered Part of the range.

Iterator ranges list<string> words; // initialize words list // Find the first occurance of the string "ant" list<string>::iterator pos = find(words.begin(), words.end(), "ant"); if (pos!= words.end()) { list<string>::iterator end = find(pos, words.end(), "bee"); while (pos!= end) { cout << "Word is " << *pos << "\n"; i++; } }

Queues Queue is a data structure implementing First in First Out (FIFO) Many situations in programming are best implemented as a queue. Think print requests sent to a printer. See example.

Stacks Stacks implement Last in First Out (LIFO). Think of a stack of books (or people hired to a business.) Stacks also have a push and pop Instead of front stack has top See example

Sets Sets do not keep things in the order you inserted them Sets reject duplicates Major advantage of a set is that an item can be found quickly Basic operations for a set are: Adding elements Removing elements Finding elements Traversing elements Like lists, sets have iterators See example

Similarities between the containers All STL containers are templated vector<string> words; vector<point> Path; list<int> numbers; stack<vector<int>> NumberLists;

Similarities between the containers All STL containers have iterators Syntax: container_type<elements_data_type>::iterator i; Example: list<string>::iterator i;

Similarities between the containers You can use the following member functions with any STL container C: C.size() Returns number of items in the container (int). C.begin() Returns iterator to first element. C.end() Returns iterator to "sentinel" node at end.

Example You can step through a container using iterators list<string>::iterator iter = mylist.begin(); while (iter!= mylist.end()) { cout << *iter; ++iter; }

Differences between STLs Many containers have their own member functions. For example: vector<string>words(10); words[0] = "Homer"; makes sense, but list<int>numbers; numbers[0] = 7; Does not.

Differences between STLs Push and pop make sense for queues and stacks, but there is no push and pop for sets. stack<int> numbers; numbers.push(2); set<int>numbers; numbers.push(2); // error

Common algorithms Recall the <algorithm> library we used with vectors. vector<int> numbers(10); // fill the vector numbers.sort(numbers.begin(), numbers.end()); Same technique can be used to sort a deque or even an ordinary array. Sets and maps are already sorted. Lists have their own sort member function.

Common algorithms These are for vectors and deques vector<int> a(10); Fill a vector with 1s fill (a.begin(), a.end(), 1); Fill next 5 positions with 1s fill_n(a.begin(), a.end(), 5, 1); vector<int>b(20); Initialize first 10 positions of b copy((a.begin(), a.end(),b.begin());

Common algorithms sort(a.begin(),a.end()); reverse(a.begin(),a.end()); random_shuffle(a.begin(), a.end()); rotate(a.begin(), a.end());

Using functions as arguments There are many algorithms that can take a function as part of the input! bool is_even(int n) { return!(n%2); } First even number in a list list<int>::iterator = find_if(a.begin(),a.end(), is_even)

Using functions as arguments int random_100() { return 1 + rand() % 100; } generate(a.begin(), a.end(), random_100);

Using functions as arguments partition(a.begin(), a.end(), is_even);

Using functions as arguments int square(int n) { return n*n; } transform(a.begin(),a.end(),b.begin, square);