05-Library-Reference - CS106B Autumn 2010 Handout 05 C and...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106B Handout 05 Autumn 2010 January 8 th , 2010 C++ and CS106 Library Reference Written by Julie Zelenski. A couple of decent C++ web resources you might want to bookmark: http://www.cppreference.com http://www.cplusplus.com/ref/ http://msdn2.microsoft.com/en-us/library/cscc687y.aspx These can be useful for anything in standard C++, which includes the language itself and all of its standard libraries ( string , stream , ctype , math , etc.) The standard C++ string class The string class is defined in <string> . The string type is actually a typedef shorthand. The underlying full name is std::basic_string<char, std::char_traits<char>, std::allocator<char>> . You don’t need to worry about this sort of low-level goop, but you will see the full name in compiler error messages and will want to recognize it as such. The default constructor initializes a string variable to the empty string, thus declaring a string variable ensures that its contents start empty. This is unlike the built-in types ( int , double , etc.) that have random contents until explicitly initialized. Assigning one string to another via = or passing/returning a string makes a new distinct copy of the same character sequence. Strings are mutable, unlike Java strings. A string literal, i.e., sequence of characters within double-quotes such as "binky" , is actually an old-style C-string. You can typically use a C-string wherever a string object is required since there is an automatic conversion from C-string to new-style C++ string object. If ever need to force this conversion, you can do so using a syntax similar to a typecast: string("binky") . This is invoking the string class constructor that takes a C-string argument. In general, operations on strings are designed to be very efficient and, as a result, some do not check parameters for validity. It is the client's job to ensure positions/lengths are in bounds for calls to substr , find, replace, and so on. The behavior on incorrect calls is implementation-dependent, but unlikely to be pleasant in any situation.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 str . length () str . size () Returns number of characters in receiver string ( length and size are synonyms) str [ index ] str . at( index ) Access character at specified index in receiver string. Indexes start at 0. at throws an exception if out of bounds, operator [] does not bounds- check (for efficiency). str . empty () Returns true if receiver string is equal to "" , false otherwise str1 + str2 str1 + ch + is overloaded to allow strings to be concatenated with other strings and single chars. The result is a new string containing concatenation of the operands. str
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 7

05-Library-Reference - CS106B Autumn 2010 Handout 05 C and...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online