A recap of the STL and more containers Plus an intro to string and file input and output! Lecture 8.

Slides:



Advertisements
Similar presentations
M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.
C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:
Data Structures Using C++ 2E
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things –Point to the start of a range of elements (in a container)
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
Data Structures Using C++ 2E
Object Oriented Programming Elhanan Borenstein Lecture #11 copyrights © Elhanan Borenstein.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
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)
File I/O ifstreams and ofstreams Sections 11.1 &
Software Design 1.1 Tapestry classes -> STL l What’s the difference between tvector and vector  Safety and the kitchen sink What happens with t[21] on.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
COP 3530 Data Structures & Algorithms Discussion Session 3.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
I/O in C++ October 7, Junaed Sattar. Stream I/O a stream is a flow of bytes/characters/ints or any type of data input streams: to the program output.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
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.
Intro to the C++ STL Timmie Smith September 6, 2001.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objective  Standard Template Library.
Why Use Namespaces? Classes encapsulate behavior (methods) and state (member data) behind an interface Structs are similar, but with state accessible Classes.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
File Handling in C++.
CSE 332: C++ data types, input, and output Built-In (a.k.a. Native) Types in C++ int, long, short, char (signed, integer division) –unsigned versions too.
More STL Container Classes ECE Last Time Templates –Functions –Classes template void swap_val (VariableType &a, VariableType &b) { VariableType.
Lecture 14 Arguments, Classes and Files. Arguments.
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
Std Library of C++ Part 2. vector vector is a collection of objects of a single type vector is a collection of objects of a single type Each object in.
Files To read a file – We must know its name – We must open it (for reading) – Then we can read – Then we must close it That is typically done implicitly.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
Streams and IO  Streams are pipe like constructors used for providing IO.  When a programmer needs to handle input from or output to external entities,then.
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.
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
Object-Oriented Programming (OOP) Lecture No. 41
ifstreams and ofstreams
Introduction to Computers Computer Generations
Introduction to Programming
Midterm Review.
Standard Template Library (STL)
FILE INPUT OUTPUT Skill Area 315 Part B Materials Prepared by
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
From lab 1 Using cs lab machines (including remote)
Containers, Iterators, Algorithms, Thrust
STL and Example.
Why Use Namespaces? Classes encapsulate behavior (methods) and state (member data) behind an interface Structs are similar, but with state accessible Classes.
STL Containers Some other containers in the STL.
File I/O.
STL and Example.
ifstreams and ofstreams
Text Files.
Presentation transcript:

A recap of the STL and more containers Plus an intro to string and file input and output! Lecture 8

Standard template library (STL)  General purpose library of data structures and algorithms  Everything in the STL is a template  Containers e.g.  Containers e.g.  Algorithms e.g. sort, find, for_each, search  Iterators  Generalization of pointers  Access data within containers  Reverse, Forward and random

Sequential containers  Containers:  std::vector v (dynamic array)  std::list l (doubly linked list)  std::deque dq (double ended queue)  Adaptors:  std::queue q  std::stack s  std::priority_queue pq (A heap)

 Container adaptors are made specifically to behave in a certain way  stack – last in first out  queue –first in first out

Associative containers  Set – Unique keys, specific order, think of it as a binary search tree  std::set first;  Map – Unique keys, specific order, key value pair, binary search tree implementation  Operator [] allows access to get value by key

Map Map  std::map mymap; mymap[0]=“hello”;mymap[1]=“random”;  mymap.insert (0, “hello”));  std::map newmap; newmap.insert newmap.insert

Multi key containers  std:: ms  Same as a set but allows keys of the same value to exist  std:: mm  Same as a map but allows multiple keys of the same value

Streams(part of the standard C++ library)  – input output stream objects  ostream – output stream objects  istream – input stream objects  istream  ifstream – input file stream  istringstream – input string stream  ostream  ofstream – output file stream  ostringstream – output string stream

<sstream>  Provides both input and output  << input data  >> output data std::stringstream ss; ss << 5 << “hello”; int f; string s; ss >> f; //now has value 5 ss >> s; //now has value “hello”

<fstream>  Similar to sstream  ofstream myfile; myfile.open(“test.txt”); //open a file myfile << “hello word\n”; //write into the file myfile.close(); //close the file  ofstream myfile (“test.txt”);

Reading from file  ifstream myfile; myfile.open(“test.txt”); //open a file string line; while ( getline (myfile,line) ) { cout << line << '\n'; } myfile.close(); myfile.close(); //close the file