Bruce Eckel's Thinking in C++, 2nd Ed Contents | Prev | Next

17: Strings

[48]One of the biggest time-wasters in C is character arrays: keeping 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.

(This is the general problem of shallow copy vs. deep copy .) 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 see how C++ strings accommodate variation in character sets and string data conversion.

Handling text is perhaps one of the oldest of all programming applications, so it’s not surprising that the C++ string draws heavily on the ideas and terminology that have long been used for this purpose in C and other languages. As you begin to acquaint yourself with C++ strings this fact should be reassuring, in the respect that no matter what programming idiom you choose, there are really only about three things you can 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, and translate between various schemes for representing string characters.

You’ll see how each of these jobs is accomplished using C++ string objects.


[48] Much of the material in this chapter was originally created by Nancy Nicolaisen

Contents | Prev | Next


Contact: webmaster@codeguru.com
CodeGuru - the website for developers.
[an error occurred while processing this directive]