Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
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. | Chapter 13 – C++ String Class String objects 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 Declaring string Objects Use the class name string and list object names Example: string s1, s2, s3; s1, s2, s3 would be string objects String header needed to declare string objects Do NOT specify size (Note: no brackets) Initializing string Objects Can initialize with the = C++ automatically reserves sufficient memory Can also place in ( ) but still need " " Null character not required for string text Data member of string class stores size of text Lesson 13.1 s1 = "This is an example."; Operators + string Objects Lesson 13.1 Type Operator Action Assignment = Stores string += Concatenates and stores Comparison == True if strings identical != True if strings not identical > True if first string greater than second True if first string greater than second = True if first string greater or equal than second > For input and string objects << For output and string objects Character [ ] To access individual characters access Concatenation + Connects two strings 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 Some Member Functions More actions needed than operators can provide Calling member function involves using object name with dot operator and function name Invoking object One that is modified Lesson 13.2 find Function Searches for a string within a string Basic form of call to find ob1.find (ob2); finds first occurrence of string ob2 within ob1 Returns position Lesson 13.2 s1 = "This is an example."; s2 = "exam"; n = s1.find (s2); 0 1 2 3 4 5 6 7 8 9 0 1 The return value is 11 .