Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |
String processing with character arrays is one of the biggest
time wasters in C. Character arrays require the programmer to keep track of the
difference between static quoted strings and arrays created on the stack and
the heap, and the fact that sometimes you re passing around a char* and
sometimes you must copy the whole array.
Especially because string manipulation is so common,
character arrays are a great source of misunderstandings and bugs. Despite
this, creating string classes remained a common exercise for beginning C++ programmers
for many years. The Standard C++ library string class solves the problem of character array manipulation once and for all, keeping track of memory even during
assignments and copy-constructions. You simply don t need to think about it.
This chapter examines
the Standard C++ string class, beginning with a look at what constitutes
a C++ string and how the C++ version differs from a traditional C character
array. You ll learn about operations and manipulations using string
objects, and you ll see how C++ strings accommodate variation in
character sets and string data conversion.
Handling text is one of the oldest programming applications,
so it s not surprising that the C++ string draws heavily on the ideas and
terminology that have long been used in C and other languages. As you begin to
acquaint yourself with C++ strings, this fact should be reassuring. No
matter which programming idiom you choose, there are three common things you
want to do with a string:
Create or modify the sequence of characters stored in the string.
Detect the presence or absence of elements within the string.
Translate between various schemes for representing string
characters.
You ll see how each of these jobs is accomplished using C++ string
objects.
Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |