Laboratory Exercises, C++ Programming
Laboratory Exercises, C++ Programming
Laboratory Exercises, C++ Programming
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Strings and Streams 23<br />
3 Strings and Streams<br />
Objective: to practice using the string class and the stream classes.<br />
Read:<br />
• Book: strings, streams, function templates.<br />
1 Class string<br />
1.1 Introduction<br />
In C, a string is a null-terminated array of characters. This representation is the cause of many<br />
errors: overwriting array bounds, trying to access arrays through uninitialized or incorrect<br />
pointers, and leaving dangling pointers after an array has been deallocated. The <br />
library contains some useful operations on C-strings, such as copying and comparing strings.<br />
<strong>C++</strong> strings hide the physical representation of the sequence of characters. A <strong>C++</strong> string<br />
object knows its starting location in memory, its contents (the characters), its length, and the<br />
length to which it can grow before it must resize its internal data buffer (i.e., its capacity).<br />
The exact implementation of the string class is not defined by the <strong>C++</strong> standard. The<br />
specification in the standard is intended to be flexible enough to allow different implementations,<br />
yet guarantee predictable behavior.<br />
The string identifier is not actually a class, but a typedef for a specialized template:<br />
typedef std::basic_string string;<br />
So string is a string containing characters of the type char. There is another standard string<br />
specialization, wstring, which describes strings of characters of the type wchar t. wchar t is the<br />
type representing “wide characters”. The standard does not specify the size of these characters;<br />
usually it’s 16 or 32 bits (32 in gcc). In this lab we will ignore all “internationalization” problems<br />
and assume that all characters fit in one byte. Strings in <strong>C++</strong> are not limited to the classes string<br />
and wstring; you may specify strings of “characters” of any type, such as int. However, this is<br />
discouraged: there are other sequence containers that are better suited for non-char types.<br />
Since we’re only interested in the string specialization of basic string, you may wonder why<br />
we mention basic string at all? There are two reasons: First, std::basic string is all the<br />
compiler knows about in all but the first few translation passes. Hence, diagnostic messages only<br />
mention std::basic string. Secondly, it is important for the general understanding of the<br />
ideas behind the string method implementations to have a grasp of the basics of a <strong>C++</strong> concept<br />
called character traits, i.e., character “characteristics”. For now, we will just show parts of the<br />
definition of basic string:<br />
/* Standard header */<br />
namespace std {<br />
template<br />
class basic_string {<br />
// types:<br />
typedef traits_t traits_type;<br />
typedef typename traits_t::char_type char_type;<br />
typedef unsigned long size_type;<br />
static const size_type npos = -1;<br />
// much more<br />
};<br />
}