Data Type string::size_type
Many C++ string member functions return a numeric value which is not of type int. The member functions size() and length() return a value which has type string::size_type. This is an unsigned integer type. (An unsigned integer can never be negative.) Suppose I want to store the length in a variable. I must declare the variable. It is more precise to declare it as type string::size_type rather than int. Note that to use this data type, size_type must be preceded by the word string and two colons:
string state = "New York";
string city = "Cincinnati";
string::size_type num1, num2 ;
num1 = state.length(); num1 is 8 (the space is counted)
num2 = city.size(); num2 is 10
Notes:
• We are using the names of the strings--state and city--to indicate what string object [variable] the member function should act upon. [to whom the function belongs?]
• It is safest to use string::size_type when referring to string sizes or to positions in a string.
Post A Comment:
0 comments: