Standard library types Practical session # 3 Software Engineering 094219.

Slides:



Advertisements
Similar presentations
1 Arrays and Strings Chapter 9 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Advertisements

Chapter 10.
STL. What is STL? Standard Templates Library Templates are best used for –Defining containers for storing data –Some kinds of algorithms that work the.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
C++ for Engineers and Scientists Third Edition
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 16 Exceptions,
Chapter 8 Arrays and Strings
Computer Science 1620 Strings. Programs are often called upon to store and manipulate text word processors chat databases webpages etc.
C++ fundamentals.
C++ crash course Class 3 designing C++ programs, classes, control structures.
Data Structures Using C++ 2E
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
Working with Strings Lecture 2 Hartmut Kaiser
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
1 Data Structures A Data Structure is an arrangement of data in memory. A Data Structure is an arrangement of data in memory.  The purpose is to map real.
Chapter 8 Arrays and Strings
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
Chapter 1 Working with strings. Objectives Understand simple programs using character strings and the string library. Get acquainted with declarations,
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Working with Batches of Data Lecture 4 Hartmut Kaiser
Looping and Counting Lecture 3 Hartmut Kaiser
An Introduction to STL. The C++ Standard Template Libraries  In 1990, Alex Stepanov and Meng Lee of Hewlett Packard Laboratories extended C++ with a.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
#include guards Practical session #2 Software Engineering
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
Chapter 3 Working with Batches of Data. Objectives Understand vector class and how it can be used to collect, store and manipulate data. Become familiar.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Why Use Namespaces? Classes encapsulate behavior (methods) and state (member data) behind an interface Structs are similar, but with state accessible Classes.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
 2003 Prentice Hall, Inc. All rights reserved vector Sequence Container Declarations –std::vector v; type : int, float, etc. Iterators –std::vector.
Object-Oriented Programming (OOP) and C++
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
CS212: Object Oriented Analysis and Design
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Popping Items Off a Stack Using a Function Lesson xx
Chapter 1.2 Introduction to C++ Programming
Fundamentals of Characters and Strings
Working with Strings Lecture 2 Hartmut Kaiser
Starting Out with C++ Early Objects Eighth Edition
Collections Intro What is the STL? Templates, collections, & iterators
One-Dimensional Array Introduction Lesson xx
7 Arrays.
Popping Items Off a Stack Lesson xx
Console input.
Working with Strings Lecture 2 Hartmut Kaiser
Arrays Kingdom of Saudi Arabia
Class string and String Stream Processing
ㅎㅎ Fifth step for Learning C++ Programming Pointers Homework solution
7 Arrays.
Arrays Arrays A few types Structures of related data items
C++ Programming Lecture 20 Strings
Today’s Objectives 28-Jun-2006 Announcements
Collections Intro What is the STL? Templates, collections, & iterators
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

Standard library types Practical session # 3 Software Engineering

Standard library  The standard C++ library is a collection of functions, constants, classes, objects and templates.  Extends the C++ language providing basic functionality to perform several tasks, like classes to interact with the operating system, data containers, manipulators to operate with them and algorithms commonly needed.  Further information can be found in:   BLACK BOX 2

Agenda  vector and iterator  string 3

vector  vector is a sequential container similar to an array.  Further information in :  vector is a template (To Be Defined), meaning: vector definition must specify its type.  std::vector v;  vector is a part C++ Standard Template Library, we don’t care how it is implemented, we can use its functions knowing only their description (i.e. interface).  #include  std::vector v;  vector can be dynamically resized (in contrary to arrays ).  v.push_back(num); 4

vector #include int main(){ std::vector v; //read numbers into a vector int num = 0; while (std::cin >> num ){ v.push_back(num); } for (std::vector ::size_type i = 0 ; i < v.size() ; ++i ){ std::cout << v[i] << std::endl; } return 0; } 5 When will this loop stop? Non int value entered OR End- Of-File value

Multi-dimensional vectors  There is no such thing!  What one can use is vector-of-vectors: std::vector > v(5); creates vector of 5 elements, each of them an empty vector of ints  To add a new (empty) row: v.push_back(vector ());  To add an int to a row: v[1].push_back(100);  To access an element (assumes it exists): v[i][j] = 23; 6 Note the space whitespace!

vector - iterator  A type that provides generic way for accessing sequencial container’s elements.  Further information :  Specific to vector’s type.  std::vector ::iterator iter1;  Has a pointer like behavior (iterator != pointer).  std::cout<<*iter; //print value  if ( (*iter1)==(*iter2) )... //comparing values  If ( iter1==iter2 )… // comparing addresses  Used by a number of functions.  std::vector v1;  std::vector ::iterator iter = v1.begin(); 7

vector - iterator #include int ia[] = { 51, 23, 7, 88, 41 }; int main () { // Initializing vector using ptr to begin and end std::vector vec( ia, ia + sizeof(ia)/sizeof(int) ); // printing the vector for (std::vector ::size_type i = 0 ; i < vec.size() ; i++ ){ std::cout<< vec[i] << ", " ; } //another way to print for ( std::vector ::iterator iter = vec.begin(); iter != vec.end(); iter++ ){ std::cout<< (*iter) << ", " ; } 8

vector – const_iterator  std::vector ::const_iterator citer;  const std:: vector ::iterator iter;  const_iterator – doesn’t not allow to change the vector, but allows to iterate over a vector. std::vector ::const_iterator citer = vec.begin(); *citer = 5; //ERROR citer++; //OK  iterator defined as const – doesn’t allow to point to another location at the vector, but allows to change the value pointed. const std::vector ::iterator iter = vec.begin(); *iter = 7; //OK iter++; //ERROR 9 Are NOT the same *citer is const iter is const

Agenda  vector and iterator  string 10

string  A special type of container, designed to operate with sequences of characters.  Further information : std::string stringOne( “my name” );  Unlike C-strings (char*) – string is a class which allows many built-in, powerful features.  Manages it’s own memory – easy and effective resizing. stringOne.append(“is John”); stringOne.push_back(‘!’);  Easy iteration, search and comparison. string::iterator it = stringOne.begin(); 11

string – examples What will be the output? #include void main() { std::string stringOne("Hello World"); //init std::string stringTwo(stringOne),stringThree; int i; stringTwo.append(" Again."); // equivalent to += std::cout << stringOne << std::endl; std::cout << stringTwo << std::endl; std::cout << stringOne + " " + stringTwo << etd::endl; std::cout << "Enter your name: "; std::cin >> stringThree; std::cout << "Hello " << stringThree << std::endl; } 12

string - examples (2) std::string k,m,n,o,p; m = "David"; n = "and"; o = "Goliath"; k = "Solomon"; std::cout<< k << std::endl; std::cout<< m <<" "<< n <<" "<< o << std::endl; p = m + " " + n + " " + o; std::cout << p << std::endl; p.append(" meet at last!"); std::cout << p << std::endl; m = "Beware the ides of March"; n = m.substr(11,4); o = m.substr(19); p = m + n + o; std::cout << p << std::endl; std::cout << p.find(“hides”) << std::endl; 13 Returns std::string::npos if substring isn’t found

Input parsing  Suppose we want to get from the user the following data:  Year (into int)  book title (into string)  (if exists) book subtitle (into string)  “No subtitle” should be denoted by empty subtitle string  First attempt: 14

Input parsing int year; std::string bookTitle, bookSubtitle; std::cout << "Enter year" << std::endl; std::cin >> year; std::cout << "Enter book title" << std::endl; std::cin >> bookTitle; std::cout << "Enter book subtitle" << std::endl; std::cin >> bookSubtitle; std::cout << "Year:" << year << std::endl; std::cout << "Title:" << bookTitle << std::endl; std::cout << "Subtitle:" << bookSubtitle << std::endl;  Look OK, right? 15

Input parsing Enter year 1984 Enter book title The good year Enter book subtitle Year:1984 Title:The Subtitle:good Press any key to continue...  What happened???  “>>” operator stops at whitespaces, and can’t read empty strings!  To read a string “as is”, we have to use the getline function 16

Input parsing – second try int year; std::string bookTitle, bookSubtitle; std::cout << "Enter year" << std::endl; std::cin >> year; std::cout << "Enter book title" << std::endl; getline(std::cin, bookTitle); std::cout << "Enter book subtitle" << std::endl; getline(std::cin, bookSubtitle); std::cout << "Year:" << year << std::endl; std::cout << "Title:" << bookTitle << std::endl; std::cout << "Subtitle:" << bookSubtitle <<std::endl;  Looks OK, right? 17

Input parsing – second try Enter year 1984 Enter book title Enter book subtitle The good year Year:1984 Title: Subtitle:The good year Press any key to continue...  Why did we skip title?  ‘>>’ doesn’t remove trailing newlines!  Arghhhh!!!! 18

Input parsing - solution  Let’s go back to what we wanted in the first place  We wanted the user to input three lines - the first for the year, the second for the title and the third for subtitle.  We know how to read a single line into a string – with getline  But how do we read an integer (year) from this string?  We apply ‘>>’ to it!  To apply ‘>>’, we need to wrap the string with a ‘ istringstream ’ (defined in ). 19

 istringstream provides an interface to manipulate strings as input streams.  Using only cin object diagram: ?  Using also istringstream object (wrapper) diagram: OK std::istringstream Input parsing - solution 20 std::cin std::string std::cin std::string

Input parsing - solution int year; std::string yearStr, bookTitle, bookSubtitle; std::cout << "Enter year" << std::endl; getline(std::cin, yearStr); std::istringstream input(yearStr); input >> year; std::cout << "Enter book title" << std::endl; getline(std::cin, bookTitle); std::cout << "Enter book subtitle" << std::endl; getline(std::cin, bookSubtitle); std::cout << "Year:" << year << std::endl; std::cout << "Title:" << bookTitle << std::endl; std::cout << "Subtitle:" << bookSubtitle << std::endl; 21

Input parsing - solution Enter year 1984 Enter book title The good year Enter book subtitle Year:1984 Title:The good year Subtitle: Press any key to continue...  That’s what we wanted!  What will happen if we put in year? 2001July? July2001? 22