02_strings_vectors_recursion - CSCI-1200 Data Structures...

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

View Full Document Right Arrow Icon
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() . This seems like a lot to remember. Do I need to memorize this? Where can I find all the details on string objects?
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.2 Exercises 1. What will be the values of strings a , b and c at the end of the following code fragment: std::string a, b, c; std::cin >> a >> b >> c; for the input: all-cows eat123 grass. every good boy deserves fudge!
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 ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern