tailieunhanh - A Complete Guide to Programming in C++ part 19
A Complete Guide to Programming in C++ part 19. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | COMPARING STRINGS 159 The comparative operators were overloaded in the string class to allow easy comparison of strings. This also allows you to use strings to formulate the conditions for branches and loops. Example strl and str2 are objects of type string if strl str2 strl is less than str2 Results of Comparisons Strings are compared lexicographically that is character by character beginning at the first character. To decide whether a single character is smaller greater or identical to another character the character codes of the character set are compared. Thus if you are using the ASCII character set the letter A ASCII code 65 is smaller than the letter a ASCII code 97 . A comparison results in a bool type value. Given two strings si and s2 si s2 is true only if both strings are identical this requires that both strings are exactly the same length. si s2 is true only if the first character in si that differs from the corresponding character in s2 is smaller than the corresponding character in s2 or if s2 is simply an extension of si. All other comparative operations can be deduced from the above rules. For example the expression si s2 is true only if s2 si is also true. In an expression comparing strings one operand can again be a string constant or a single character. Example while key y . . . This example compares the string key with the single character y . This is an alternative method of expressing the comparison key y . String comparisons can also be combined to form more complex expressions. Example while key y key Y . . . The controlling expression is valid if the string key contains only the letter Y or y . Due to the higher precedence of the comparative operator versus the operator no parentheses are required in this example. 160 CHAPTER 9 THE STANDARD CLASS STRING INSERTING AND ERASING IN STRINGS Inserting a string string s1 Miss Summer 5 Ashley Insert at position 5 Effect of the statement Erasing a substring string s The summer-time .
đang nạp các trang xem trước