Date post: | 05-Jan-2016 |
Category: |
Documents |
Upload: | richard-wade |
View: | 226 times |
Download: | 4 times |
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 ActionAssignment = Stores string
+= Concatenates and storesComparison == True if strings identical
!= True if strings not identical > True if first string greater than second < True if first string is less than second>= True if first string greater or equal than second<= True if first string less or equal than second
Input/Output >> For input and string objects<< For output and string objects
Character [ ] To access individual characters accessConcatenation + 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
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
replace Function
Replaces characters within a string object with another string
Returns reference to invoking object Basic form: ob1.replace (index, num, ob2);
– index represents position to begin– num is the number of characters to replace– ob2 what is to be used to replace (can be C string)
Overloaded: ob1.replace (index1, num1, ob2, index2, num2);
Lesson 13.2
portion of ob2 to use
erase Function
Eliminates characters within string object Basic 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.";
insert Function
Adds characters to a string object Basic form: ob1.insert (index, ob2);
– index is beginning position– ob2 represents what is to be inserted
Can be a C string
Returns reference to the invoking function
Lesson 13.2
s1 = "This is an example.";s1.insert (8,"just ");
just an example.";
Other string Functions
Many other functions available– Examples: compare, append, resize, etc.
Described in text, table 13.2– List of functions– Sample syntax– What they return– Description of purpose
Lesson 13.2
Reading a Single Word
From keyboard using cin cin >> 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 <string> 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 < num_lines; j++) { getline (infile, array_element); }
Lesson 13.3
Strings and Functions
Return type for function is string object string objects passed like other objects
– Copy is passed when type string is argument Keyword const prevents array elements
from being modified & indicates a reference
Lesson 13.4
string function1 (string, const string[ ], string&, string [ ]);
Class Definition: Example
Lesson 13.5
class Class1 { private: char aa [20]; string s1; public: char* get_aa( ); string get_s1 ( ); void read_data ( ); };
Both C strings and string objects can be members
of a class
"get" functions returnprivate data memberFunction reads both
data members
Member Functions
Lesson 13.5
class Class1 { private: char aa [20]; string s1; public: char* get_aa( ); string get_s1 ( ); void read_data ( ); };
void Class1 :: read_data ( ) { cout << "Enter your name." << endl; getline (cin ,s1); cout<< "Enter phone number." <<endl; cin.getline (aa) }
char* Class1 :: get_aa ( ) { return aa; }
string Class1 :: get_s1 ( ) { return s1; }
Working With a Class
Object of class declared – allows member functions to be called
Example:
Class1 ob1;ob1.read_data ( );cout <<ob1.get_s1<<endl<<ob1.get_aa ( ) <<endl;
Lesson 13.5
Summary
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
Learned how to: