< Previous | Contents | Next >

Using the size() Member

Function

Okay, its time to take a look at a string member function. Next, I use the member function size():


cout << "The phrase has " << phrase.size() << " characters in it.\n\n";


phrase.size() calls the member function size() of the string object phrase through the member selection operator . (the dot). The size() member function simply returns an unsigned integer value of the size of the string objectits number of characters. Because the string object is "Lame Over!!!", the member function returns 12. (Every character counts, including spaces.) Of course, calling size() for another string object might return a different result based on the number of characters in the string object.


Hi n t

image

string objects also have a member function length(), which, just like size(), returns the number of characters in the string object.

image

Using String Objects 93