string
Class StringsLife is a bit simpler if you use string
class strings instead of C-style strings because the class design allows you to use relational operators to make the comparisons. This is possible because one can define class functions that “overload,” or redefine, operators. Chapter 12, “Classes and Dynamic Memory Allocation,” discusses how to incorporate this feature into class designs, but from a practical standpoint, all you need to know now is that you can use the relational operators with string
class objects. Listing 5.12 revises Listing 5.11 to use a string
object instead of an array of char
.
// compstr2.cpp -- comparing strings using arrays
#include <iostream>
#include <string> // string class
int main()
{
using namespace std;
string word = "?ate";
for (char ch = 'a'; word != "mate"; ch++)
{
cout << word << endl;
word[0] = ch;
}
cout << "After loop ends, word is " << word << endl;
return 0;
}
The output from the program in Listing 5.12 is the same as that for the program in Listing 5.11.
In Listing 5.12, the following test condition uses a relational operator with a string
object on the left and a C-style string on the right:
word != "mate"
The way the string
class overloads the !=
operator allows you to use it as long as at least one of the operands is a string
object; the remaining operand can be either a string
object or a C-style string.
The string
class design allows you to use a string
object as a single entity, as in the relational test expression, or as an aggregate object for which you can use array notation to extract individual characters.
As you can see, you can achieve the same results with C-style strings as with string
objects, but programming with string
objects is simpler and more intuitive.
Finally, unlike most of the for
loops you have seen to this point, the last two loops aren’t counting loops. That is, they don’t execute a block of statements a specified number of times. Instead, each of these loops watches for a particular circumstance (word
being "mate"
) to signal that it’s time to stop. More typically, C++ programs use while
loops for this second kind of test, so let’s examine that form next.
while
LoopThe while
loop is a for
loop stripped of the initialization and update parts; it has just a test condition and a body:
while (test-condition)
body
First, a program evaluates the parenthesized test-condition
expression. If the expression evaluates to true
, the program executes the statement(s) in the body. As with a for
loop, the body consists of a single statement or a block defined by paired braces. After it finishes with the body, the program returns to the test condition and re-evaluates it. If the condition is nonzero, the program executes the body again. This cycle of testing and execution continues until the test condition evaluates to false
(see Figure 5.3). Clearly, if you want the loop to terminate eventually, something within the loop body must do something to affect the test-condition
expression. For example, the loop can increment a variable used in the test condition or read a new value from keyboard input. Like the for
loop, the while
loop is an entry-condition loop. Thus, if test-condition
evaluates to false
at the beginning, the program never executes the body of the loop.
Listing 5.13 puts a while
loop to work. The loop cycles through each character in a string and displays the character and its ASCII code. The loop quits when it reaches the null character. This technique of stepping through a string character-by-character until reaching the null character is a standard C++ method for processing C-style strings. Because a string contains its own termination marker, programs often don’t need explicit information about how long a string is.
// while.cpp -- introducing the while loop
#include <iostream>
const int ArSize = 20;
int main()
{
using namespace std;
char name[ArSize];
cout << "Your first name, please: ";
cin >> name;
cout << "Here is your name, verticalized and ASCIIized:
";
int i = 0; // start at beginning of string
while (name[i] != ' ') // process to end of string
{
cout << name[i] << ": " << int(name[i]) << endl;
i++; // don't forget this step
}
return 0;
}
Here is a sample run of the program in Listing 5.13:
Your first name, please: Muffy
Here is your name, verticalized and ASCIIized:
M: 77
u: 117
f: 102
f: 102
y: 121
(No, verticalized and ASCIIized are not real words or even good would-be words. But they do add an endearing technoid tone to the output.)
18.118.24.106