《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 9 Strings

Chapter 9 ABSOLUTE C++ Strings WALTER SAVITCH SECOND EDITION PEARSON Copyright2006 Pearson Addison-Wesley All rights reserved
Chapter 9 Strings

Learning Objectives An Array Type for Strings ◆C-Strings Character Manipulation Tools ◆Character I/o get,put member functions putback,peek,ignore Standard Class string ◆String processing Copyright 2006 Pearson Addison-Wesley.All rights reserved. 9-2
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-2 Learning Objectives ¨ An Array Type for Strings ¨C-Strings ¨ Character Manipulation Tools ¨Character I/O ¨get, put member functions ¨putback, peek, ignore ¨ Standard Class string ¨String processing

Introduction ◆Two string types: C-strings Array with base type char End of string marked with null,"\O" "Older"method inherited from C ◆String class ◆Uses templates Copyright006 Pearson Addison-Wesley.All rights reserved. 9-3
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-3 Introduction ¨ Two string types: ¨ C-strings ¨Array with base type char ¨End of string marked with null, "\0" ¨"Older" method inherited from C ¨ String class ¨Uses templates

C-Strings Array with base type char One character per indexed variable One extra character:"10" Called "null character" ◆End marker ◆We've used c-strings Literal "Hello"stored as c-string Copyright 2006 Pearson Addison-Wesley.All rights reserved. 9-4
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-4 C-Strings ¨ Array with base type char ¨One character per indexed variable ¨One extra character: "\0" ¨Called "null character" ¨End marker ¨ We’ve used c-strings ¨Literal "Hello" stored as c-string

C-String Variable ◆Array of characters: char s[10]; Declares a c-string variable to hold up to 9 characters ◆+one null character Typically "partially-filled"array Declare large enough to hold max-size string Indicate end with null Only difference from standard array: Must contain null character Copyright006 Pearson Addison-Wesley.All rights reserved. 9-5
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-5 C-String Variable ¨ Array of characters: char s[10]; ¨ Declares a c-string variable to hold up to 9 characters ¨ + one null character ¨ Typically "partially-filled" array ¨ Declare large enough to hold max-size string ¨ Indicate end with null ¨ Only difference from standard array: ¨ Must contain null character

C-String Storage .A standard array: char s[10]; If s contains string "Hi Mom",stored as: ◆Display,page370 s[ol s[i] s[2] s[3】 s4] s[s] s[6] sl7l s[8] sl9] H M 0 m o 2 Copyright 2006 Pearson Addison-Wesley.All rights reserved. 9-6
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-6 C-String Storage ¨ A standard array: char s[10]; ¨If s contains string "Hi Mom" , stored as: ¨Display, page 370

C-String Initialization Can initialize c-string: char myMessage[20]"Hi there."; Needn't fill entire array Initialization places "\O"at end ◆Can omit array-size: char shortString[]"abc"; Automatically makes size one more than length of quoted string ◆NOT same as: char shortString {"a","b","c"}; Copyright 2006 Pearson Addison-Wesley.All rights reserved. 97
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-7 C-String Initialization ¨ Can initialize c-string: char myMessage[20] = "Hi there."; ¨ Needn’t fill entire array ¨ Initialization places "\0" at end ¨ Can omit array-size: char shortString[] = "abc"; ¨ Automatically makes size one more than length of quoted string ¨ NOT same as: char shortString[] = {"a" , "b" , "c"};

C-String Indexes ◆Ac-string IS an array Can access indexed variables of: char ourString[5]"Hi"; ◆ourString[o]is"H" ◆ourString[1]is"i ◆ourString[2]is"n0" +ourString[3]is unknown ourString[4]is unknown Copyright 2006 Pearson Addison-Wesley.All rights reserved. 9-8
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-8 C-String Indexes ¨ A c-string IS an array ¨ Can access indexed variables of: char ourString[5] = "Hi"; ¨ourString[0] is "H" ¨ourString[1] is "i" ¨ourString[2] is "\0" ¨ourString[3] is unknown ¨ourString[4] is unknown

C-String Index Manipulation Can manipulate indexed variables char happyString[7]="DoBeDo"; happyString[6]"Z"; ◆Be careful! +Here,"\O"(null)was overwritten by a "Z"! If null overwritten,c-string no longer "acts"like c-string! Unpredictable results! Copyright006 Pearson Addison-Wesley.All rights reserved. 9-9
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-9 C-String Index Manipulation ¨ Can manipulate indexed variables char happyString[7] = "DoBeDo"; happyString[6] = "Z"; ¨Be careful! ¨Here, "\0" (null) was overwritten by a "Z"! ¨ If null overwritten, c-string no longer "acts" like c-string! ¨Unpredictable results!

Library ◆Declaring c-strings Requires no C++library Built into standard C++ ◆Manipulations Require library Typically included when using c-strings Normally want to do "fun"things with them Copyright 2006 Pearson Addison-Wesley.All rights reserved. 9-10
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 9-10 Library ¨ Declaring c-strings ¨Requires no C++ library ¨Built into standard C++ ¨ Manipulations ¨Require library ¨Typically included when using c-strings ¨Normally want to do "fun" things with them
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 10 Pointers and Dynamic Arrays.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 12 Streams and File IO.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 11 Separate Compilation and Namespaces.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 5 Arrays.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 8 Operator Overloading, Friends, and References.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 7 Constructors and Other Tools.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 6 Structures and Classes.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 2 Flow of Control.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 3 Function Basics.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 1 C++ Basics.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 4 Parameters and Overloading.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第三章.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第二章 信源与信息熵.ppt
- 西安邮电大学:《信息论与编码》课程教学课件(PPT讲稿)第一章 绪论(主讲:王军选).ppt
- 《信息论与编码》课程教学资源(作业习题)第三章 信道与信道容量(含解答).pdf
- 《信息论与编码》课程教学资源(作业习题)自测题无答案.pdf
- 《信息论与编码》课程教学实验指导书.pdf
- 《信息论与编码》课程教学大纲 Element of Information Theory and Coding B.pdf
- 《信息论与编码》课程教学大纲 Element of Information Theory and Coding A.pdf
- 《信息安全概论》课程教学资源(PPT课件)第9章 信息安全标准与法律法规.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 13 Inheritance.ppt
- 《C++面向对象程序设计》课程教学资源(PPT课件)Chapter 14 Polymorphism and Virtual Functions.ppt
- 《微机技术及应用》课程教学大纲 Microcmputer Technology and aplications.doc
- 《微型计算机技术及应用》课程电子教案(PPT教学课件,共十五章,完整版).pptx
- 《计算机导论》课程教学大纲 Computer Concepts.pdf
- 《计算机导论》课程教学课件(英文讲稿)1-a-Computer History+ Di Devices.pdf
- 《计算机导论》课程教学课件(英文讲稿)1-b-Digital Data Representation.pdf
- 《计算机导论》课程教学课件(英文讲稿)2-a-Computer Hardware.pdf
- 《计算机导论》课程教学课件(英文讲稿)2-b-Computer Hardware.pdf
- 《计算机导论》课程教学课件(英文讲稿)3-a-b-Computer Software.pdf
- 《计算机导论》课程教学课件(英文讲稿)4- operating system.pdf
- 《计算机导论》课程教学课件(英文讲稿)4-a- File mangement.pdf
- 《计算机导论》课程教学课件(英文讲稿)5-a- LANS_WANS.pdf
- 《计算机导论》课程教学课件(英文讲稿)5-b- LANS_WANS.pdf
- 《计算机导论》课程教学课件(英文讲稿)6-a- The Internet.pdf
- 《计算机导论》课程教学课件(英文讲稿)6-b- The Internet.pdf
- 《计算机导论》课程教学课件(英文讲稿)7-a- Web.pdf
- 《C语言程序设计》课程教学资源(讲义资料)C语言中详解指针.doc
- 《C语言程序设计》课程教学资源(讲义资料)C指针详解(经典详细).pdf
- 《C语言程序设计》课程教学资源(讲义资料)C语言指针用法详解.pdf