CS197c: Programming in C++

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

STL: C++ Standard Library

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

CPSC 427a: Object-Oriented Programming

Standard Template Library

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

COEN244: Class & function templates

Containers in C++ and Java

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

STL Standard Template Library

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

The Standard Template Library Classes

CSI33 Data Structures

Lectures 11,12. Online documentation & links

Dynamic Data Structures

Module 9. Templates & STL

Chapter 5. The Standard Template Library.

List, Stack, and Queues

use static size for this buffer

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

The Standard Template Library. An introduction

G52CPP C++ Programming Lecture 18

G52CPP C++ Programming Lecture 15

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

6.096 Introduction to C++

Templates and Vectors

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

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

Standard Template Library

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

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

CS11 Advanced C++ Fall Lecture 1

PIC 10A. Lecture 23: Intro to STL containers

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

Lecture-5. STL Containers & Iterators

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

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

Programming with Haiku

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

Exceptions, Templates, and the STL

More on Templates. Shahram Rahatlou. Corso di Programmazione++

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

7 TEMPLATES AND STL. 7.1 Function Templates

Function Templates. Consider the following function:

SSE2034: System Software Experiment 3

Computational Physics

CS 103 Unit 12 Slides

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

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

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

1. The term STL stands for?

TABLE OF CONTENTS...2 INTRODUCTION...3

Abstract Data Types 1

Associative Containers and Iterators. Ali Malik

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

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

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

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

19.1 The Standard Template Library

Unit 4 Basic Collections

CSCE 110 PROGRAMMING FUNDAMENTALS

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

CSC 222: Computer Programming II. Spring 2004

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

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

Teaching with the STL

CSE 333 Lecture 9 - intro to C++

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

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

Abstract Data Types 1

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

Introduction to C++ Introduction to C++ 1

Short Notes of CS201

Lecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1

CSE 100: C++ TEMPLATES AND ITERATORS

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

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

CS201 - Introduction to Programming Glossary By

2

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

COMP322 - Introduction to C++

Vectors. CIS 15 : Spring 2007

W3101: Programming Languages C++ Ramana Isukapalli

Suppose that you want to use two libraries with a bunch of useful classes and functions, but some names collide:

Chapter 17: Linked Lists

General Advise: Don t reinvent the wheel

G Programming Languages Spring 2010 Lecture 11. Robert Soulé, New York University

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

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

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

CSE 100: C++ TEMPLATES AND ITERATORS

Vector. Vector Class. class Vector { public: typedef unsigned int size_type;

C++: Overview and Features

Inheritance, and Polymorphism.

1 Short Answer (7 Points Each)

Lesson 13 - Vectors Dynamic Data Storage

Transcription:

CS197c: Programming in C++ Lecture 2 Marc Cartright http://ciir.cs.umass.edu/~irmarc/cs197c/index.html

Administration HW1 will be up this afternoon Written assignment Due in class next week See website for details on formatting

Syllabus lecture 1 : C++ basics & tools lecture 2 : Standard Template Library lecture 3 : STL again (algorithms) lecture 4 : Pointers lecture 5 : More Pointers lecture 6 : Memory Management lecture 7 : Advanced Topics lecture 8 : Conclusion

Today s lecture Brief Intro to Classes and Headers, then: Standard Template Library (STL)

C++ Classes Conceptually similar to Java classes Mostly syntax differences: class Foo { // member declarations here }; // method definitions out here

C/C++ Headers Header files are interface files for C/C++ C:.h C++:.h is optional Examples: #include <stdio.h> #include <iostream> #include <stdlib.h> #include <cstdlib> // C-style header // C++-style // C header, updated...

C++ Headers and Classes Usually only function declarations, type defs Foo.h: class Foo { private: int id; string name; public: Foo(string n); //constructor ~Foo(); //destructor string getname() { return name; } //inlined! };

C++ Headers and Classes Source contains function/method definitions: Foo.cpp: #include Foo.h Foo::Foo(string n) { name = n; } Foo::~Foo() {} string Foo::getName() { return name; } // or define it here

What is STL??? A library of template based reusable components Implements common data structures and algorithms Uses generic programming based on templates Similar to generics in Java

So what do templates look like? There are function templates: template <class T> void quickprint(t t) { cout << t << endl; } quickprint(4.5); quickprint( fourteen );

So what do templates look like? and class templates template <class T> class Foo { // declarations bool contains(t &item); }; template <class T> bool Foo<T>::contains(T &item) { //definition using T }

So what is the STL made up of? Containers Data structures that store objects of any type Iterators tool used to traverse elements in containers Algorithms common algorithms like searching and sorting

Why use STL at all?? Code has been tested for decades Solid, efficient code base Provides interfaces which are easy to use Do not have to mess with internal mechanism of how the data structures are implemented

Containers Sequence containers Linear data structures E.g. lists, vectors, deque Associative containers Non-linear containers E.g. maps, multimaps, sets, multisets Container adapters Constrained sequence containers E.g., stacks and queues

Iterators Iterators are pointers to elements of sequence and associative containers Type const_iterator defines iterator to a container element that cannot be modified Type iterator defines iterator to a container element that can be modified All sequence and associative containers provide the begin() and end() methods Returns iterators pointing to the first and one after the last element in the container

Iterators (cont..) If it points to a element in the container it++ (++it) points to the next element in the container (*it) is the actual element in the container The iterator resulting from end() can only be used to detect whether the iterator has reached the end of the container.

Sequence containers Commonalities assume an ordering of elements allows sorting Examples: vector (based on array) queue (based on...queue) deque (double-ended queue) list (based on linked lists)

Vectors based on arrays Allow direct access to its elements using an overloaded [] operator Insertion at the end is efficient Use the push_back method Insertion and deletion in the middle is expensive An entire portion of the vector needs to be removed

Example of using a vector /* creating a vector of ints */ /* display the content of v */ #include <iostream> #include <vector> using namespace std; int main() { vector<int> v; vector<int>::iterator it; v.push_back(2); v.push_back(3); v.push_back(4); } for(it = v.begin(); it!= v.end(); it++) { cout<<(*it)<<endl; } cout << the size of the vector is ; cout << v.size() << endl; cout << the capacity of ; cout << the vector is ; cout << v.capacity() << endl;

Some methods for sequences size() capacity() insert(iterator, const T & x) erase(iterator) pop_back() reserve() clear()

Lists Implemented using doubly linked list Insertion and deletion are efficient at any point of the list Bidirectional iterators are used to traverse the list in both directions

Deque Stands for double-ended queue Combines the benefits of list and vector Provides indexed access using indexes (not possible in lists) Provides efficient insertion and deletion (not efficient in vectors)

Associative containers Associative containers use keys to store and retrieve elements There are four types of associative containers Set, multiset, map, multimap All associative containers maintain keys in sorted order All associative containers support bidirectional iterators

Mulitsets Multisets are implemented using red-black trees Multiset allows duplicate keys The ordering is determined by the STL comparator function object less<t> Keys sorted by less<t> must support the comparison operator

Example of multiset #include <iostream> #include <set> main() { } multiset<int, less<int> > ms; ms.insert(10); ms.insert(10); cout<<ms.count(10); multiset<int, less<int> > ::iterator it = ms.find(10); if(it!= ms.end()) cout<< 10 was found <<endl;

Sets Exactly like multisets But does not allow duplicate keys

Multimaps associate keys to values implemented using red-black trees insertion is done using objects of the class pair the ordering is determined by the STL comparator function object less<t>

Multimaps example #include <iostream> #include <map> int main() { multimap<int, double> mp; mp.insert(pair<int,double>(10,14.5)); multimap<int, double> ::iterator it; for(it=mp.begin(); it!= mp.end();it++) cout<<it->first<< <<it->second; }

Container Adapters (Stacks) LIFO data structure They are implemented with vector, list, and deque(by default) Header file <stack> Examples stack using vector : stack<int, vector<int>> s1 stack using list : stack<int, list<int>> s2 Stack using deque : stack<int> s3;

Other things not covered Algorithms in STL sorting, searching etc. Generic well implemented algorithms Try and use them

Next lecture STL Algorithms and more examples of the STL