Today’s Class
• Class string– The basics– Assignment– Concatenation– Compare & swap– Find– Conversion to C-style char * strings– Iterators
Reminders
• Final Exam– Thursday, December 19th, 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
Class String
• Template class basic_string– String manipulation operations such as copying,
searching, etc.– Template definition in namespace std
• typedef basic_string< char > string;
• Need the following in a file– #include <string>– using std::string;– string str;
Constructors
• Have several different ways of initializing a string (see 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
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';
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
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
I/O
• Stream extraction• cin >> str;
– Delimited by whitespace characters
• getline( cin, str);– Delimited by newline
• Stream insertion• cout << str;
Assignment
• Have several different ways– (See assignment.txt)string str1("string"), str2, str3,
str4, str5;
str2 = str1;
str3.assign(str1);
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];
Concatenation
• Also many ways to do thisstr3 += str1 + "SSSSS";
//str3 = stringSSSSS
str4.append(str1 + "SSSSS");
//str4 = stringSSSSS
str5.append(str1, 1, 4);
//str5 = stringtrin
//(str1’s 1st to 4th elements)
string str6(str1 + " and " + str2);
//str6 = string and string
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'
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 examples
Characteristics (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
Find Functions (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
Convert to C-style Strings
• Have to take several steps (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
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
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;
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
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
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