1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Programming in C++ - Chapter 13: C++ string class

21 91 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 21
Dung lượng 406,28 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

On completion of chapter 13 students will know how to: Create strings with the C++ string class, manipulate strings with string functions, read a word, or multiple lines from keyboard, use the string class in programs.

Trang 1

Chapter 13 – C++ String Class

Trang 2

Do not need to specify size of string object

– C++ keeps track of size of text

– C++ expands memory region to store text as  needed

Can use operators to perform some string manipulations

Lesson 13.1

Trang 3

Declaring  string Objects

Use the class name string and list object names

Trang 4

s1 = "This is an example.";

Trang 6

C Strings vs.  string Objects

Older code uses C strings

C strings more basic – faster execution

String class improves ability to manipulate text safely

– Sized automatically

– No null necessary

– Easier modification in program

Lesson 13.1

Trang 7

– Invoking object

One that is modified Lesson 13.2

Trang 9

Overloaded  find Function

Another version has two arguments

Basic form:        ob1.find (ob2, index);

– index represents integer value for beginning of  search

C++ performs automatic type conversion from C strings to string objects when C 

strings are in string function call

String not found returns ­1

Lesson 13.2

Trang 10

Replaces characters within a string object with another string

Trang 11

Eliminates characters within string objectBasic form:   ob1.erase (index, num);

– num represents number of characters to erase – index is the beginning position

Returns reference to the invoking object

Lesson 13.2

s1 = "This is an example.";

s1.erase (8,3);example.";      

Trang 14

Lesson 13.3

Trang 15

Lesson 13.3

Trang 18

Lesson 13.5

class Class1         {

Trang 19

       <<  endl;

       getline (cin ,s1);

       cout<< "Enter phone number."       <<endl;

       cin.getline (aa)        }

      return s1;

      }

Trang 21

Create strings with the C++ string classManipulate strings with string functionsRead a word, or multiple lines from 

keyboard

Use the string class in programs

Learned how to:

Ngày đăng: 30/01/2020, 02:56

TỪ KHÓA LIÊN QUAN