 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer 2003 1 Chapter 15 - Class string and String Stream.

Slides:



Advertisements
Similar presentations
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Advertisements

Data Structures Using C++ 2E
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Today’s Class Class string –The basics –Assignment –Concatenation –Compare & swap –Find –Conversion to C-style char * strings –Iterators.
 2006 Pearson Education, Inc. All rights reserved Class string and String Stream Processing.
計算機概論實習 How to Use string Template class basic_string String manipulation (copying, searching, etc.) typedef basic_string string; Also typedef.
 2006 Pearson Education, Inc. All rights reserved Class string and String Stream Processing.
 2006 Pearson Education, Inc. All rights reserved Standard Template Library (STL)
 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
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,
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
Templates and the STL.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
Data Structures Using C++ 2E
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 16: Exceptions,
Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,
 2000 Prentice Hall, Inc. All rights reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction 15.2C A Simple Program: Adding Two Integers.
Data Structures Using C++1 Chapter 4 Standard Template Library (STL)
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
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.
Chapter 22 STL Containers §22.1 STL Basics §22.2 STL Iterators §22.3 Sequence Containers §22.4 Associative Containers §22.5 Container Adapters.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
Lecture 7 : Intro. to STL (Standard Template Library)
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 15 - Class string and String Stream Processing Outline 15.1 Introduction 15.2 string Assignment.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Standard Template Library (STL) - Use Vector and Deque
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Template, Standard Template Library Lecture 9 March 22, 2005.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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)
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
 2003 Prentice Hall, Inc. All rights reserved vector Sequence Container Declarations –std::vector v; type : int, float, etc. Iterators –std::vector.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 21 - Standard Template Library (STL) Outline 21.1 Introduction to the Standard Template Library.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Object-Oriented Programming (OOP) Lecture No. 42.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
Object-Oriented Programming (OOP) Lecture No. 41
Class string and String Stream Processing: A Deeper Look
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
Chapter 22: Standard Template Library (STL)
C++ STRINGS string is part of the Standard C++ Library
CS212: Object Oriented Analysis and Design
Class string and String Stream Processing
Standard Template Library
Today’s Objectives 28-Jun-2006 Announcements
Standard Template Library
4.1 Introduction Arrays A few types Structures of related data items
Standard Template Library
Presentation transcript:

 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 Processing Outline 15.1 Introduction 15.2 string Assignment and Concatenation Outline 21.1 Introduction to the Standard Template Library (STL) Introduction to Containers Introduction to Iterators Chapter 21 - Standard Template Library (STL)

 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Introduction Template class basic_string –Provides string manipulation (copying, searching, etc.) typedef basic_string string; Creates the alias string for basic_string –To use string, include the header file string initialization string s1( "Hello" ); string s2( 8, 'x' ); // 8 'x' characters string month = "March" Implicitly calls constructor

 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Introduction No conversion from int or char –The following definitions are errors string error1 = 'c'; string error2( 'u' ); string error3 = 22; string error4( 8 ); –However, can assign to a single char to a string object is permissible if declared as s = 'n';

 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Introduction string features –Not necessarily null terminated –length member function: s1.length() –Use [] to access individual characters: s1[0] 0 to length-1 –Many member functions take start position and length If length argument too large, max chosen –Stream extraction and getline operator are overloaded cin >> stringObject; getline( cin, s) –Reads an input from the keyboard, delimited by a newline

 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer string Assignment and Concatenation Examples of member functions: assignment s2 = s1; Makes a separate copy s2.assign(s1); Same as s2 = s1; myString.assign(s, start, N); Copies N characters from s, beginning at index start –Individual characters s2[0] = s3[2];

 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer string Assignment and Concatenation Range checking s3.at( index ); Returns character at index Concatenation s3.append( "pet" ); s3 += "pet"; Both add "pet" to end of s3 s3.append( s1, start, N ); Appends N characters from s1, beginning at index start

 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Introduction to the Standard Template Library (STL) STL –Powerful, template-based components Containers: template data structures Iterators: like pointers, access elements of containers Algorithms: data manipulation, searching, sorting, etc. –Object- oriented programming: reuse, reuse, reuse –Only an introduction to STL, a huge class library

 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Introduction to Containers Three types of containers –Sequence containers Linear data structures (vectors, linked lists) Referred to as a first-class container –Associative containers Non-linear, can find elements quickly Key/value pairs Referred to as a first-class container –Container adapters stacks (allows a program to view a container) Near containers –Similar to containers, with reduced functionality Containers have some common functions

 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer STL Container Classes (Fig. 21.1) Sequence containers vector deque list Associative containers set multiset map multimap Container adapters stack queue priority_queue

 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Common STL Member Functions (Fig. 21.2) Member functions for all containers –Default constructor, copy constructor, destructor empty max_size, size = >= == != swap Functions for first-class containers begin, end rbegin, rend erase, clear

 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Introduction to Iterators Iterator objects are similar to pointers –Point to first element in a container –Iterator operators same for all containers * dereferences ++ points to next element begin() returns iterator to first element end() returns iterator to last element –Use iterators with sequences (also called ranges) such as Containers Input sequences: istream_iterator Output sequences: ostream_iterator

 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Introduction to Iterators Usage std::istream_iterator inputInt( cin ) Can read int input from cin *inputInt –Dereference to read first int from cin ++inputInt –Go to next int in stream std::ostream_iterator outputInt(cout) Can output int s to cout *outputInt = 7 –Outputs 7 to cout ++outputInt –Advances iterator so we can output next int

 2003 Prentice Hall, Inc. All rights reserved. Outline 13 fig21_05.cpp (1 of 2) 1 // Fig. 21.5: fig21_05.cpp 2 // Demonstrating input and output with iterators. 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include // ostream_iterator and istream_iterator int main() 12 { 13 cout << "Enter two integers: "; // create istream_iterator for reading int values from cin 16 std::istream_iterator inputInt( cin ); int number1 = *inputInt; // read int from standard input 19 ++inputInt; // move iterator to next input value 20 int number2 = *inputInt; // read int from standard input 21 Note creation of istream_iterator. For compilation reasons, we use std:: rather than a using statement. Access and assign the iterator like a pointer.

 2003 Prentice Hall, Inc. All rights reserved. Outline 14 fig21_05.cpp (2 of 2) fig21_05.cpp output (1 of 1) 22 // create ostream_iterator for writing int values to cout 23 std::ostream_iterator outputInt( cout ); cout << "The sum is: "; 26 *outputInt = number1 + number2; // output result to cout 27 cout << endl; return 0; } // end main Enter two integers: The sum is: 37 Create an ostream_iterator is similar. Assigning to this iterator outputs to cout.

 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Iterator Categories (Fig. 21.6) Input –Read elements from container, can only move forward Output –Write elements to container, only forward Forward –Combines input and output, retains position –Multi-pass (can pass through sequence twice) Bidirectional –Like forward, but can move backwards as well Random access –Like bidirectional, but can also jump to any element

 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Iterator Types Supported (Fig. 21.8) Sequence containers vector : random access deque : random access list : bidirectional Associative containers (all bidirectional) set multiset Map multimap Container adapters (no iterators supported) stack queue priority_queue