Docsity

C++ String Type: From C to C++, Papers of Programming Languages

Toggle sidebar
bg1
- 1 -
C++ Strings
Prof. Allen Holliday
October 16, 2001
Introduction
It’s often mentioned that one of the failings of the C language is the lack of a true string
type, one that can be assigned, manipulated, and compared. Many early C++ books show
how to define and use a String class that provided the common operations that other data
types have. This paper contains a brief description of the string type defined by the C++
Standard Library.
References
The primary reference for this paper is “The C++ Standard Library: A Tutorial and
Reference” by Nicolai M Josuttis (Addison-Wesley, 1999, ISBN 0-201-37926-0).
Strings are mentioned in the texts for CPSC-121 and CPSC-131, but the treatment is brief
and incomplete.
Strings In C
In C, we say that a declaration like this one is a string:
char s[10];
and that this one is for a pointer to a string:
char* sp;
Neither statement is quite correct. C doesn’t have a string type. The first declaration is for
an array of characters, which is not the same. The C convention is that strings of
characters are represented by these arrays, ending with a null. However, we should
remember that a data type is defined by two things—the values it can take on and the
operations we can perform on it. It’s the second part of the definition that the C
convention doesn’t meet.
Suppose we have the following declarations:
char s1[10];
char s2[10];
char* sp1;
char* sp2;
We can’t assign the string in s1 to s2 by saying:
s2 = s1;
The C compiler would complain about the left-hand side being something that can’t be
assigned. We have to use a library function like this:
strcpy(s2, s1);
pf3
pf4
pf5

Partial preview of the text

Download C++ String Type: From C to C++ and more Papers Programming Languages in PDF only on Docsity!

C++ Strings Prof. Allen Holliday

October 16, 2001 Introduction It’s often mentioned that one of the failings of the C language is the lack of a true string type, one that can be assigned, manipulated, and compared. Many early C++ books show how to define and use a String class that provided the common operations that other data types have. This paper contains a brief description of the string type defined by the C++ Standard Library. References The primary reference for this paper is “The C++ Standard Library: A Tutorial and Reference” by Nicolai M Josuttis (Addison-Wesley, 1999, ISBN 0- 201 - 37926 - 0). Strings are mentioned in the texts for CPSC-121 and CPSC-131, but the treatment is brief and incomplete. Strings In C In C, we say that a declaration like this one is a string: char s[10]; and that this one is for a pointer to a string: char* sp; Neither statement is quite correct. C doesn’t have a string type. The first declaration is for an array of characters, which is not the same. The C convention is that strings of characters are represented by these arrays, ending with a null. However, we should remember that a data type is defined by two things—the values it can take on and the operations we can perform on it. It’s the second part of the definition that the C convention doesn’t meet. Suppose we have the following declarations: char s1[10]; char s2[10]; char* sp1; char* sp2; We can’t assign the string in s1 to s2 by saying: s2 = s1; The C compiler would complain about the left-hand side being something that can’t be assigned. We have to use a library function like this: strcpy(s2, s1);

Similarly, we can’t compare two character arrays by saying: if (s1 == s2) or: if (s1 < s2) even though we’d often like to. In this case, the compiler would not complain, but it would compare the starting addresses of the two arrays, not the strings in the arrays. We have to use another library function: strcmp(s1, s2) and test the return value to see if the strings are equal or which one is less than the other. The pointers have the same problem. If we said: Sp2 = sp1; The C compiler wouldn’t complain, but it would only copy the pointer itself, not the string being pointed to. C++ Strings The C++ language allows the definition of new data types and the overloading of normal operators to provide convenient operations for them. ISO-compliant C++ compilers include a Standard Library which give you a wide variety of useful data types (vectors, lists, trees, and so forth). We’ll discuss one of the most useful—the string. To use the C++ string, your program must include its header file and specify its namespace: #include using std::string; You may then declare instances of strings just as you would any other type: string s string s string* sp1; string* sp The Standard Library defines the string class and implements all of its operations; all you have to do is use them. These strings can be used in ways very similar to other types such as ints and floats. You can say: s1 = s2; and the compiler will copy one string to another.

Mixing C and C++ Strings You may be faced with a situation where you have to use both string types; for instance, if you are updating some legacy code or using a library that expects C strings. If you have these declarations: char c[11]; string s; you can assign the character array to the string very easily: s = c; The opposite isn’t quite so easy; an array can’t be assigned to. Instead, you have to use the string’s c_str() conversion function and the old strcpy library function: strcpy(c, s.c_str()); You can use the c_str() function to pass a string to any function that expects a pointer to a C string; it returns a char * to the C++ string’s character string. For example, you can say: open(name.c_str(),…); String Functions The Standard Library provides a large number of functions, too numerous to discuss here. There is a powerful set of find functions, which you use to find a single character, a substring, one of a given set of characters and even one that isn’t in a given set. These functions can start forward from the string’s beginning and find the first occurrence of their target, or backwards from the end and find the last occurrence. Some can start from any specified position in the string. The library also provides a family of insert and erase functions that can operate on any specified part of a string. String IO Reading and writing strings is quite similar to IO for other data types. You can read a string from your terminal by saying: cin >> s; and write to your terminal by saying: cout << s; Strings can also be read from and written to disk files. The details are beyond the scope of this paper. You should refer to my handout “C++ Files,” which includes a discussion of string IO. Dynamic Memory Allocation An issue with C strings is dynamic allocation of memory. If we use a pointer, we must be sure to allocate enough memory to hold the longest string. Consider the following program fragment:

Char* sp; . . sp = malloc(maximumSize); strcpy(sp, s1); In the above example, the strcpy writes wherever sp points. If the malloc were absent and sp contained some harmful value (the address of your program’s instructions or other data), the results could be quite serious. The C++ string class handles memory allocation without being requested to do so. When a string needs to grow because of an assignment or an append, the class expands the string, reallocating memory and copying the old string to its new location if necessary. This automatic allocation is even done when strings are used for reading from files. Again, you should refer to my handout “C++ Files.”