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

Similar documents
CS2141 Software Development using C/C++ Stream I/O

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

File I/O Christian Schumacher, Info1 D-MAVT 2013

COMP322 - Introduction to C++

G52CPP C++ Programming Lecture 17

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

CS11 Advanced C++ Lecture 2 Fall

Introduction to C++ (Extensions to C)

G52CPP C++ Programming Lecture 18

Bruce Merry. IOI Training Dec 2013

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Advanced I/O Concepts

CS3157: Advanced Programming. Outline

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

UEE1303(1070) S 12 Object-Oriented Programming in C++

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams

Streams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

CPSC 427a: Object-Oriented Programming

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

Streams. Rupesh Nasre.

Input/Output Streams: Customizing

CS 247: Software Engineering Principles. ADT Design

Fall 2017 CISC/CMPE320 9/27/2017

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

Chapter 15 - C++ As A "Better C"

I/O Streams and Standard I/O Devices (cont d.)

Streams in C++ Stream concept. Reference information. Stream type declarations

Review: C++ Basic Concepts. Dr. Yingwu Zhu

C++ Input/Output: Streams

Templates and Vectors

CSCI 104 Overview. Mark Redekopp David Kempe

COMP322 - Introduction to C++

CS11 Advanced C++ Fall Lecture 1

More Advanced Class Concepts

Chapter 12. Streams and File I/O. Copyright 2016 Pearson, Inc. All rights reserved.

Topics. bool and string types input/output library functions comments memory allocation templates classes

struct Buffer { Buffer(int s) { buf = new char[s]; } ~Buffer() { delete [] buf; } char *buf; };

W3101: Programming Languages C++ Ramana Isukapalli

10/23/02 21:20:33 IO_Examples

I/O streams

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Chapter 11 Customizing I/O

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

PIC 10A. Lecture 23: Intro to STL containers

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Strings and Stream I/O

Due Date: See Blackboard

Come and join us at WebLyceum

Introduction to Programming using C++

C++ basics Getting started with, and Data Types.

Documentation. Programming / Documentation Slide 42

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

Note 11/13/2014. They are like those i s, j s, and temp s that appear and disappear when the function starts and finishes...

1. The term STL stands for?

PIC10B/1 Winter 2014 Exam I Study Guide

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

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Major Differences between Java and C++ Programming in C++ Multiple Inheritance

Chapter 11 Customizing I/O

Ali Malik Handout 12 CS106L Winter 2018 Jan 30th, C++ Reference

Lecture 10. To use try, throw, and catch Constructors and destructors Standard exception hierarchy new failures

Fibonacci in Lisp. Computer Programming: Skills & Concepts (CP1) Programming Languages. Varieties of Programing Language

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

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

CS 103 Unit 14 - Streams

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

CSE 303: Concepts and Tools for Software Development

A <Basic> C++ Course

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York

Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects

Chapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved

A <Basic> C++ Course

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

pointers & references

Objects and streams and files CS427: Elements of Software Engineering

Scientific Computing

CS201 Solved MCQs.

CS103L SPRING 2018 UNIT 7: FILE I/O

CS 103 Unit 14 - Streams

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Lecture on pointers, references, and arrays and vectors

C++_ MARKS 40 MIN

4 Strings and Streams. Testing.

Come and join us at WebLyceum

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

Chapter 2. Procedural Programming

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

Object Oriented Programming using C++

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Chapter 11 Customizing I/O

Searching Algorithms. Chapter 11

Transcription:

Standard Library Lecture 27 Containers (templates) Streams (I/O facilities) Standard Library Portable, type-safe, efficient Try to use as much as possible Heavy use of templates Streams: #include <sstream> #include <fstream> #include <iostream> Containers: #include <vector> #include <list> #include <queue> #include <stack> using name space std; 1 2 Containers A container is an object that holds other objects(lists, vectors, associative arrays) Container-iterator model Simple and efficiency Commonality provided through iterators Different than the based object approach of Java (everything inherits from base class Object) STL Containers Vector: basically a dynamic array Iterators: c.begin(); (first element of a container) c.end(); (last element of a container) ++p (next element) vector<int> vi(10); // create a vector of 10 integers Each element is initizlied by default constructor (then one that has no arguments) 3 4

Vector continued Incrementally adding to a vector v[i] // uchecked access (no range checking) v.at(i) // checks range (throws exception) Vectors are typically large so in many cases they are passed as references: void f1(vector<int>& ); // can modify vector void f2(const vector(int>&); // can not modify vector void f3(vector<int> ); // uncommon inefficient // copy constructors will be called // for each vector element 5 In C realloc would be used vector<point> cities; void add_points(point sentinel) Point buf; while (cin >> buf) if (buf == sentinel) return; // check new point cities.push_back(buf); 6 Vector operates like a stack. New element is added at the end (same as:) cities.insert(cities.end(), buf); Also: cities.erase(cities.begin()+1); Sequences Other sequences are very similar to vector so I will not cover them (list, queue, dequeue etc) vector<int> numbers; numbers.push_back(1); numbers.push_back(2);... vector<int>::iterator vi; for (vi = numbers.begin(); vi!= numbers.end(); ++vi) cout << Number = << *vi << endl; Associative Containers A map is a sequence of (key,value) pairs (dictionary) map<string, int> tbl; map<string, MapSiste *, less<string> > neighbors_; void f(map<string, number>& phone_book) typedef map<string,number>::const_iterator CI; for (CI p = phone_book.begin(); p!= phone_book.end(); ++p) cout << p->first << \t << p -> second << '\n'; 7 8

An example input: nail 100 hammer 2 saw 3 saw 4 hammer 7 nail 1000 nail 250 output: sum for each item void readitems(map<string, int>& m) string word; int val = 0; while (cin >> word >> val) m[word] += val; // subscripting with string Operations find(li.begin(), li.end(), 42) first occurance of value find_if first match of predicate Many more - basically similar to the standard functional programming stuff but more primitive Higher-order functions can be simulated by function objects (not covered in this class) 9 10 #include <string> string v1,v2; v1 = Hello v2 = world ; string v3 = v1 + v2; // convert to C style string v3.c_str(); Strings A string is a sequence of characters. Can be initialized by C-style string and C-style strings can be assigned to strings. Try to use as much as possible Much safer, better, easier than the C-style mess of string handling Streams Main idea: I/O conversion of objects of types such as int, char *, Employee into sequences of characters Ability to overload for user-defined types Formatting Files and Streams 11 12

One of my favorite ideas in C++ The usual way: put(cerr, x = ); put(cerr, x); put(cerr, '\n'); The C++ way: cerr << x = << x << '\n'; User can overload operator<< and operator>> Output of user-defined Types class Complex public: double real() const return re; double imag() const return im; ostream& operator<<(ostream& s, const complex& z) return s << ( << z.real() <<, << z.imag() << ) ; 13 14 Virtual output functions ostream members are not virtual (for reasons of efficiency) Sometimes you want to output an object for which only a base class is known. Solution: class My_base P virtual ostream& put(ostream &s) const = 0; // write *this ostream& operator<<(ostream& s, const My_base& r) return r.put(s); class SomeType: public My_base ostream& put(ostream& s) const; ; void f(const My_base& r, Sometype& s) cout << r << s; // calls right put() int x; string v; cin >> x >> v; Input is similar Will read an integer and a string seperated by whitespace (when reading from stdin enter must be pressed) Input of user defined types: istream& operator>>(istream& s, complex& a)... 15 16

Formatting Control over how the object is printed Streams have formatting state State remains after function call For integers: cout.setf(ios_base::oct, ios_base::basefiled); // octal For floating point numbers: cout.precision(8); // 8 digits cout.precision(4); // 4 digits Another way (Manipulators) : cout << setprecision(4) << angle << endl; File Streams and String Streams #include <fstream> #include <sstream> ifstream from( test.txt ); ofstream to( out.txt ); while (from.get(ch)) to.put(ch); from << Hello world << endl; string message(int x) ostringstream oss; oss << Message << x << endl; return oss.str(); void word_per_line(const string& s) istringstream ist(s); string w; while (ist >> w) cout << w << '\n'; 17 18