2003 Prentice Hall, Inc. All Rights Reserved
2003 Prentice Hall, Inc. All Rights Reserved
15.1 Introduction
15.1 Introduction
15.1 Introduction
• string features
– Not necessarily null terminated
– length member function: s1.length()
– Use [] to access individual characters: s1[0]
• 0 to length-1
– string not a pointer
– Many member functions take start position and length
• If length argument too large, max chosen
– Stream extraction
• cin >> stringObject;
• getline( cin, s)
– Delimited by newline
• Assignment
– s2 = s1;
• Makes a separate copy
– s2.assign(s1);
• Same as s2 = s1;
– myString.assign(s, start, N);
• Copies N characters from s, beginning at index start
– Individual characters
• s2[0] = s3[2];
• Range checking
– s3.at( index );
• Returns character at index
• Can throw out_of_range exception
– [] has no range checking
• Concatenation
– s3.append( "pet" );
– s3 += "pet";
• Both add "pet" to end of s3
– s3.append( s1, start, N );
• Appends N characters from s1, beginning at index start
string1: cat
fig15_01.cpp
string2: cat (3 of 3)
string3: cat
fig15_01.cpp
After modification of string2 and string3: output (1 of 1)
string1: cat
string2: rat
string3: car
After concatenation:
string1: catacomb
string2: rat
string3: carpet
string4: catapult
string5: comb
• Overloaded operators
– ==, !=, <, >, <= and >=
– Return bool
• s1.compare(s2)
– Returns positive if s1 lexicographically greater
• Compares letter by letter
• 'B' lexicographically greater than 'A'
– Returns negative if less, zero if equal
– s1.compare(start, length, s2, start,
length)
• Compare portions of s1 and s2
– s1.compare(start, length, s2)
• Compare portion of s1 with all of s2
2003 Prentice Hall, Inc. All rights reserved.
11
1 // Fig. 15.2: fig15_02.cpp
2 // Demonstrating string comparison capabilities.
Outline
3 #include <iostream>
4
fig15_02.cpp
5 using std::cout;
(1 of 4)
6 using std::endl;
7
8 #include <string>
9
10 using std::string;
11
12 int main()
13 {
14 string string1( "Testing the comparison functions." );
15 string string2( "Hello" );
16 string string3( "stinger" );
17 string string4( string2 );
18
15.4 Substrings
plane
• s1.swap(s2);
– Switch contents of two strings
• Member functions
– s1.size() and s1.length()
• Number of characters in string
– s1.capacity()
• Number of elements that can be stored without reallocation
– s1.max_size()
• Maximum possible string size
– s1.empty()
• Returns true if empty
– s1.resize(newlength)
• Resizes string to newlength
• Find functions
– If found, index returned
– If not found, string::npos returned
• Public static constant in class string
– s1.find( s2 )
– s1.rfind( s2 )
• Searches right-to-left
– s1.find_first_of( s2 )
• Returns first occurrence of any character in s2
• s1.find_frist_of( "abcd" )
– Returns index of first 'a', 'b', 'c' or 'd'
• Find functions
– s1.find_last_of( s2 )
• Finds last occurrence of any character in s2
– s1.find_first_not_of( s2 )
• Finds first character NOT in s2
– s1.find_last_not_of( s2 )
• Finds last character NOT in s2
Original string:
noon is 12 p.m.
(find) "is" was found at: 5
(rfind) "is" was found at: 5
(find_first_of) found 'o' from the group "misop" at: 1
(find_last_of) found 'm' from the group "misop" at: 13
(find_first_not_of) '1' is not contained in "noi spm" and was found at:8
(find_first_not_of) '.' is not contained in "12noi spm" and was found
at:12
find_first_not_of("noon is 12 p.m.") returned: -1
• s1.erase( start )
– Erase from index start to end of string, including start
• Replace
– s1.replace( begin, N, s2)
• begin: index in s1 to start replacing
• N: number of characters to replace
• s2: replacement string
– s1.replace( begin, N, s2, index, num )
• index: element in s2 where replacement begins
• num: number of elements to use when replacing
– Replacement can overwrite characters
– string::npos represents max string length
• s1.insert( index, s2 )
– Inserts s2 before position index
• s1.insert( index, s2, index2,
N );
– Inserts substring of s2 before position index
– Substring is N characters, starting at index2
Initial strings:
string1: beginning end
string2: middle
string3: 12345678
string4: xx
Strings after insert:
string1: beginning middle end
string2: middle
string3: 123xx45678
string4: xx
• Conversion functions
– strings not necessarily null-terminated
– s1.copy( ptr, N, index )
• Copies N characters into the array ptr
• Starts at location index
• Need to null terminate
– s1.c_str()
• Returns const char *
• Null terminated
– s1.data()
• Returns const char *
• NOT null-terminated
string s is STRINGS
string1 converted to a C-Style string is STRINGS
ptr1 is STRINGS
ptr2 is STRINGS
15.11 Iterators
• Iterators
– Forwards and backwards traversal of strings
– Access to individual characters
– Similar to pointer operations
– More coverage Chapter 20
15.11 Iterators
• Basic usage
– Creation
• string::const_iterator i = s.begin();
• const, cannot modify string (more Chapter 20)
– Referencing
• *i; // reference character
• ++i; // traverse one character forward
– Test for end of string
• i != s.end()
• end returns iterator after last element of s
fig15_10.cpp
output (1 of 1)
• String output
– Ostringstream outputString;
– outputString << s1 << s2;
– Member function str
• Returns string that was output to memory
• outputString.str()
• String input
– istringstream inputString ( myString );
– inputString >> string1 >> string2
– Like reading from cin