Download presentation
Presentation is loading. Please wait.
1
Today’s Class Class string –The basics –Assignment –Concatenation –Compare & swap –Find –Conversion to C-style char * strings –Iterators
2
Reminders Final Exam –Thursday, December 19 th, 12:00 – 2:00 pm –Covers last 1/3 of class Course grade –Will be posted Friday morning –Exams = 300 points –Assignment = 250 points –Total = 550 points
3
Class String Template class basic_string –String manipulation operations such as copying, searching, etc. –Template definition in namespace std typedef basic_string string; Need the following in a file –#include –using std::string; –string str;
4
Constructors Have several different ways of initializing a string (see constructors.txt)constructors.txt string s1("cat"); string s2 = "dog"; string s3(5, 'z'); (string "zzzzz") string s4; //default constructor string s5 = s1; //copy constructor string s6(s2); //copy constructor
5
Possible Errors No conversion from int or char –These will create a compiler error string error1 = 'c'; string error2( 'u' ); string error3 = 22; string error4( 8 ); –Can only assign a character in this manner s1 = 'X'; s2.at(0) = 'X'; s3[1] = 'X';
6
Properties Different than C-style strings –The terminating NULL (‘\0’, or 0) character not necessary –Is not a pointer Has numerous member functions –Most member functions take a starting subscript location & number of characters If number of characters is too large, max chosen Will not give STATUS_ACCESS_VIOLATION or “bus error” as in C
7
Basic Functions Number of characters –str.length() returns the length of a string –A string starts a subscript 0 and ends at subscript str.length() – 1 –Can also use str.size() Access individual characters –str[5] or str.at(5) at( ) provides range checking (but [ ] does not) at( ) will throw an out_of_range exception
8
I/O Stream extraction cin >> str; –Delimited by whitespace characters getline( cin, str); –Delimited by newline Stream insertion cout << str;
9
Assignment Have several different ways –(See assignment.txt)assignment.txt string str1("string"), str2, str3, str4, str5; str2 = str1; str3.assign(str1);
10
Assignment Can assign character-by-character –But don’t forget to resize it, as a default string has 0 length str4.resize(str1.length()); for(i=0;i<str1.length();i++) str4[i]=str1[i]; str5.resize(str1.size()); for(i=0;i<str1.size();i++) str5.at(i)=str1[i];
11
Concatenation Also many ways to do this str3 += str1 + "SSSSS"; //str3 = stringSSSSS str4.append(str1 + "SSSSS"); //str4 = stringSSSSS str5.append(str1, 1, 4); //str5 = stringtrin //(str1’s 1 st to 4 th elements) string str6(str1 + " and " + str2); //str6 = string and string
12
Comparing Can use the overloaded operators >, =, <=, ==, != –Return bool values (true = 1, false = 0) –Have to keep in mind the ASCII values for characters –For example, all capital letters have a lower value than lowercase letters –So: 'A' < 'Z' < 'a' < 'z'
13
Comparing & Swapping Or use compare() function - A.compare(B) –If A equals B, returns 0 –If A is greater than B, returns positive number –If A is less than B, returns negative number Use function swap() to swap two strings –A.swap(B); See compare.txt for some examplescompare.txt
14
Class Exercise 1 What’s the output of this program? –See exercise1.txtexercise1.txt
15
Characteristics (char.txt)char.txt Member functions: size(), length() –Number of characters currently stored in the string Member function: capacity() –Number of characters that can be stored without increasing the memory capacity of the string Member function: max_size() –Largest possible string that can be stored Member function: empty() –Whether the string is empty or not
16
Find Functions (find.txt)find.txt A.find(“string”) –Attempts to find the substring in A –If found, returns the starting location of the first matching substring –If not, returns string::pos (a constant defined in class string) A.rfind(“string”) –Same, but starts on the right and searches to the left
17
Convert to C-style Strings Have to take several steps (convert.txt)convert.txt string str1("This is a string."); int len = str1.length(); char *str2 = new char[ len + 1 ]; //save space for null str1.copy( str2, len, 0 ); // copy characters out of string into allocated memory str2[ len ] = 0; //add null terminator
18
Convert to C++-style Strings At least three ways –Use a constructor string str3(str2); –Use a for loop string str4; str4.resize(len); for(int i=0;i<len;i++) str4[i]=str2[i]; –Use an iterator
19
Iterators Using an iterator to traverse a string string str5; str5.resize(len); string::iterator i = str5.begin(); k = 0; while(i != str5.end()){ *i = str2[k]; i++; k++; } cout << "str5 = "<<str5<<endl;
20
Iterators Iterators have many features in common with pointers –Used to point to the elements in a container Container = generic (template) data structures –The dereferencing operator (*) dereferences an iterator, so you can use the element to which it points –Increment (++) and decrement (--) operators are used to move to the next element of the container
21
Iterators Container functions used with iterators –begin() Returns an iterator pointing to the first element of the container –end() Returns an iterator pointing to the first element past the end of the container
22
Iterators Have two types of iterators –An object of type iterator Refers to a container object that can be modified –An object of type const_iterator Refers to a container object that cannot be modified
23
Class Exercise 2 What’s the output of this program? –See exercise2.txtexercise2.txt
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.