02_strings_vectors_recursion

02_strings_vectors_recursion - CSCI-1200 Data Structures...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSCI-1200 Data Structures — Fall 2010 Lecture 2 — Strings, Vectors and Recursion Announcements • HW 1 is available on-line through the course website. • If you have not resolved issues with the C++ environment on your laptop, please do so immediately. Today • STL Strings • Loop Invariants • STL Vectors as “smart arrays” • Basic recursion 2.1 About STL String Objects • A string is an object type defined in the standard library to contain a sequence of characters. • The string type, like all types (including int , double , char , float ), defines an interface, which includes construction (initialization), operations, functions (methods), and even other types(!). • When an object is created, a special function is run called a “constructor”, whose job it is to initialize the object. There are several ways of constructing string objects: – By default to create an empty string: std::string my_string_var; – With a specified number of instances of a single char: std::string my_string_var2(10, ’ ’); – From another string: std::string my_string_var3(my_string_var2); • The notation my_string_var.size() is a call to a function size that is defined as a member function of the string class. There is an equivalent member function called length . • Input to string objects through streams (e.g. reading from the keyboard or a file) includes the following steps: 1. The computer inputs and discards white-space characters, one at a time, until a non-white-space character is found. 2. A sequence of non-white-space characters is input and stored in the string. This overwrites anything that was already in the string. 3. Reading stops either at the end of the input or upon reaching the next white-space character (without reading it in). • The (overloaded) operator ’+’ is defined on strings. It concatenates two strings to create a third string, without changing either of the original two strings. • The assignment operation ’=’ on strings overwrites the current contents of the string. • The individual characters of a string can be accessed using the subscript operator (similar to arrays). – Subscript 0 corresponds to the first character. – For example, given std::string a = "Susan"; Then a[0] == ’S’ and a[1] == ’u’ and a[4] == ’n’ . • Strings define a special type string::size_type , which is the type returned by the string function size() (and length() ). – The :: notation means that size type is defined within the scope of the string type. – string::size_type is generally equivalent to unsigned int . – You will have compiler warnings and potential compatibility problems if you compare an int variable to a.size() ....
View Full Document

This note was uploaded on 02/21/2012 for the course CSCI 1200 taught by Professor Cutler during the Fall '08 term at Rensselaer Polytechnic Institute.

Page1 / 13

02_strings_vectors_recursion - CSCI-1200 Data Structures...

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

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