中国高校课件下载中心 》 教学资源 》 大学文库

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

文档信息
资源类别:文库
文档格式:PPT
文档页数:40
文件大小:1.22MB
团购合买:点击进入团购
内容简介
《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

刷新页面下载完整文档
VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
相关文档