Title: Strings
1Strings
2The C String Class
- C now contains standard versions of various
containers and algorithms. - The C-style strings (arrays of char) that youve
been using are not the only way of managing
character data. - C allows you to work with a string class that
eases many of the problems youve encountered
with C-style strings. - In particular, you dont have to worry about
managing memory for the string data.
3References
- The C Programming Language, 3rd Edition, by
Bjarne Stroustrup - The C Standard Library, by Nicolai M. Josuttis
- The C Standard Template Library, by Plauger,
Stepanov, Lee, and Musser
4Basics
- The basic character template class is
basic_string. - It has two specializations (generated using
typedefs), string and wstring. - string corresponds to the C-style string (I.e.,
const char). - wstring is an extension for languages that use
characters.
5string and wstring
- Normal types (unlike const char).
- You can copy, assign, and compare strings just
like you would copy, assign, and compare
primitive types (int, double) - string a Hello
- string b string(a)
- string c a
- bool d (ab)
6Where do you find the String types?
- In the header file
- (Note, no .h)
- All such functions and other names are in the
namespace std. - The basic class name is a template class,
basic_string. - So if you see error messages about
basic_string, youre really dealing with
string
7The Constructors
- string() // empty
- string(string s) // copy of s
- string(string s, int start) // substring
- string(string s, int start, int len) // substring
- string(char a) // C-string
- string(int cnt, char c) // one or more chars
- string(char beg, char end) // beg, end)
8Accessing Individual Characters
- Its almost like for a C-string.
- string s Harry
- s0 is H
- s1 is a
-
- s5 is undefined!!!! no \0 at the end
9Overloaded Operations
- is used to assign a value (char, C-string, or
string) to a string. - is used to append a string, character, or
C-string to a string. - is used to concatenate two strings or a string
with something else - Boolean operations do a dictionary order
comparison - are used for input and output. On
input, leading whitespace is skipped, and the
input terminates with whitespace or end of file.
10So you absolutely have to use the string as a
C-string
- string s Harry
- s.data() // returns s as a data array, no \0.
- s.c_str() // returns s as a C-string with \0
- int i atoi(s.c_str()) // conversion
- char carray new char80
- s.copy(carray, 79) // copies up to 79 char
11Other Operations
- swap(a, b) // swap the guts of a with b
- s.append(s2) // append s2 to s
- s.push_back(c) // append a char
- s.erase(various) // erases substrings
- s.insert(various) // inserts substrings
- s.clear() // removes all contents
- s.resize(cnt) // change the size of s to cnt
12More Operations
- s.replace(various) // replaces characters
- s.size() // how many characters?
- s.length() // how many characters?
- s.max_size() // maximum number of char?
- s.empty() // is s empty?
- s.capacity() // size without reallocation
- s.reserve(cnt) // reserves memory
13Reading text into a string
- getline(thestream, s) // Reads from thestream
(e.g., cin or a file) into the string s. Returns
a reference to thestream that can be used again. - Ignores leading whitespace (spaces, tabs, or
carriage returns) - Then reads all characters until a line delimiter
or end of file is encountered. - The line delimiter is extracted but not put into
the string - You can then parse s without worrying about end
of line or end of file characters.
14Searching strings
- s.find(substring) // finds first
- s.find(substring, start) // starting at start
- s.rfind(substring) // finds last
- These return an unsigned integer (size_t). Dont
convert this into a standard integer! Unpleasant
things can happen! - Many more find functions exist.
- stdstringnpos is returned if the substring is
not found. This is defined to have the type
size_t. Its value is (size_t)(-1), which is
funny-looking.
15Transforming a string
- Lowercase all characters
- transform(s.begin(), s.end(), s.begin(),
tolower) - Uppercase all characters
- transform(s.begin(), s.end(), s.begin(),
toupper) - tolower and toupper are C-string functions. Other
functions can also be used.
16Looping through a string
- for(size_t i 0 i
- // work with si
-
- Unpacking this code
- size_t is the type of an index into an array
- s.size() is the size of the string
- i is more efficient than i
- si is the individual character
17An Example
Extracting words and printing them
backwards include include
using namespace std int main(int argc,
char argv) const string delims(
\t,.) string line while(getline(cin,li
ne)) size_t begIdx, endIdx
18More of the Example
begIdx line.find_first_not_of(delims)
while(begIdx!stringnpos)
endIdx line.find_first_of(
delims, begIdx) if(endIdxstringnp
os) endIdx line.length()
for(int i endIdx-1 i
static_cast(begIdx) --i)
coutbegIdx line.find_first_not_of(
delims, endIdx) cout
19Conclusions
- Use strings instead of C-strings.
- If you need to use a function from the C-string
library, see the slide on transforming a string. - strings manage there own memory, so you can
declare a class or struct containing a string and
not have to worry about memory management woes.