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

21 91 0
Lecture Programming in C++ - Chapter 13: C++ string class

Đ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

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 s1 = "This is an example."; 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 Operators + string Objects Type       Operator Assignment = += Comparison == !=  >  < >= > > s1; – Reads characters until whitespace typed  – Whitespace includes space and "Enter" key Amount of memory for s1 automatically  made sufficient Lesson 13.3 Reading Multiple Lines Use function getline – Contained in  General form:  getline (cin, ob1, 'terminator'); – Ob1 is name of string object – Terminator is terminating character Read but not included in ob1 object Default value is '\n' Read single line of input:  getline (cin, ob1); Lesson 13.3 Reading an Input File Use getline for complete file – Read each line into 1­D array of string objects Use for loop General form:        for ( int j = 0; j 

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

Từ khóa liên quan

Mục lục

  • Chapter 13 – C++ String Class

  • String objects

  • Declaring string Objects

  • Initializing string Objects

  • Operators + string Objects

  • C Strings vs. string Objects

  • Some Member Functions

  • find Function

  • Overloaded find Function

  • replace Function

  • erase Function

  • insert Function

  • Other string Functions

  • Reading a Single Word

  • Reading Multiple Lines

  • Reading an Input File

  • Strings and Functions

  • Class Definition: Example

  • Member Functions

  • Working With a Class

Tài liệu cùng người dùng

Tài liệu liên quan