08-Library-Reference

08-Library-Reference - CS106X Winter 2008 Handout 08 C and CS106 Library Reference Written by Julie Zelenski You're sitting at your computer trying

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

View Full Document Right Arrow Icon
CS106X Handout 08 Winter 2008 January 14, 2008 C++ and CS106 Library Reference Written by Julie Zelenski. You're sitting at your computer, trying to write some code that manipulates a string or stream or draws some graphics. You know the functionality you need probably exists in one of the libraries, but don't remember the exact details. The information might be in the reader, but it's easy to get lost in there. You could open the header file and check it out directly. This works okay for our CS106 interfaces (which tend to be nicely commented :-), but it is not so helpful for the standard headers which are often cryptic. The web, of course, is filled with gobs of information, both handy and useless, so you can probably find what you need there but you'll need to dig around some. Then it occurred to me, how about I try to be helpful and pull together a quick summary of the most commonly used functionality for handy reference? And thus, this document was born… This is a new handout, so let me know if you do find it useful and if so, what errors/omissions I should correct for next time. Note that this is intended as a quick high-level overview, so if you need more in-depth details, check the reader/header file/web. 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.) which have random contents until explicitly initialized. Assigning one
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 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 an 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
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.

This note was uploaded on 04/18/2008 for the course CS 106X taught by Professor Cain,g during the Winter '08 term at Stanford.

Page1 / 6

08-Library-Reference - CS106X Winter 2008 Handout 08 C and CS106 Library Reference Written by Julie Zelenski You're sitting at your computer trying

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