Академический Документы
Профессиональный Документы
Культура Документы
A string is a sequence of zero or more characters. The size function tells you h
ow many characters are in the string:
string s = "Hello";
cout << s.size(); // writes 5
s = "Wow";
cout << s.size(); // writes 3
s = "";
cout << s.size(); // writes 0
(For historical reasons, there is also a length function that returns the same v
alue that size does. In other words, s.length() and s.size() may be used interch
angeably.)
You can access individual characters in a string using the [] operator. The posi
tions of the characters in a string are numbered from left to right, starting at
0. Your program's behavior is undefined if it tries to access a character at a
position that is out of range for the string.
string s = "Hello";
cout << s[0];
//
cout << s[4];
//
cout << s[5];
//
cout << s[-1];
//
// 01234
// Hello
writes H
writes o
Undefined behavior!
Undefined behavior!
// 012345678
// duplicate
// writes cat
Sometimes we want to classify characters, asking, for example, whether they are
letter characters or digit characters. If you say
#include <cctype>
then you can use character classification functions like these:
string s = "Maroon
if (isalpha(s[0]))
...
if (isupper(s[0]))
...
if (islower(s[2]))
...
if (isdigit(s[7]))
...
if (islower(s[0]))
...
if (isalpha(s[6]))
...
if (isalpha(s[7]))
...
// 01234567
5"; // Maroon 5
// true, since 'M' is a letter
// true, since 'M' is an uppercase letter
// true, since 'r' is a lowercase letter
// true, since '5' is a digit character
// false, since 'M' is not a lowercase letter
// false, since ' ' is not a letter
// false, since '5' is not a letter
Yeah!";
s.size(); k++)
// if not a letter
// append it to t
++ Standard says that s[k] in this case has undefined behavior: The program migh
t crash, or access memory that is not part of the string, or appear to work norm
ally, or appear to work for a while and then misbehave, or print a useful diagno
stic message, etc. Different compilers might do different things at different ti
mes.
When you build a Visual C++ project in the Debug configuration, which is what we
're doing, the Visual C++ 2012 library's implementation of the [] operator for s
trings will, in fact, check to see if the position is within range, and if not,
immediately terminate the program, putting up a dialog box containing the text D
ebug Assertion Failed! and string subscript out of range. This alone is useful i
n that it alerts you to a problem, but the real payoff comes if you know somethi
ng about using the Visual C++ 2012 debugger.
If your program uses the [] operator for strings and dies when run under Visual
C++ 2012, producing the dialog box with the message string subscript out of rang
e, you can click Retry to fire up the debugger. (Annoyingly, after clicking Retr
y, you will have to select "Debug the program" in the next dialog box that comes
up, and then in the next box, select the Possible Debugger that has the name of
your project, and click Yes. In the next dialog box that appears, click Break.)
In the Call Stack window, double-click the line just below the one containing "
yourProject.exe!std::basic_string<char,...". You'll then see your program code,
with an arrow in the left margin pointing to one of the lines of code. Somewhere
in the indicated line in your program code is your bad subscript; you can exami
ne the Autos window to see the values of any variables involved. Select Debug /
Stop Debugging when you're finished exploring.