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

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 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 s1 = "This is an example."; Operators + string Objects Lesson 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 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 find Function Searches for a string within a string Basic form of call to find (ob2); finds first occurrence of string ob2 within ob1 Returns position Lesson s1 = "This is an example."; s2 = "exam"; n = (s2); 0 1 2 3 4 5 6 7 8 9 0 1 The return value is 11 .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.