1 / 12

Principles of Computer Science I Honors Section

Principles of Computer Science I Honors Section. Note Set 9 CSE 1341. Today. STL String. string objects. sequence of characters much like a null-terminated c-string Pro’s easier to program with don’t have to worry about array length don’t have to deal with pointers Con’s

tuvya
Download Presentation

Principles of Computer Science I Honors Section

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Principles of Computer Science IHonors Section Note Set 9 CSE 1341

  2. Today STL String

  3. string objects • sequence of characters much like a null-terminated c-string • Pro’s • easier to program with • don’t have to worry about array length • don’t have to deal with pointers • Con’s • Heavyweight library (pre-programmed “stuff”) • Not practical for all software deployment environments

  4. Declaring a String Object #include <string> //new header file to use strings int main() { string s; string t = “CSE1341H”; string u (“CSE1341H”); }

  5. String Usage string dept = “CSE”; string course = “1341H”; string cls = dept + course; cout << cls; cls += “ is awesome!”; cout << cls;

  6. String Usage string fname; string lname; cout << “Enter first name: “; cin >> fname; cout << “Enter last name: “; cin >> lname; cout << lname << “, “ << fname;

  7. String Usage string fname; string lname; cout << “Enter first name: “; cin >> fname; cout << “Enter last name: “; cin >> lname; cout << lname << “, “ << fname;

  8. String Usage string sent; cout << "Enter something with spaces: "; getline(cin, sent); //Different cout << sent;

  9. String Object Member Functions • C++ objects put together in nice, neat little packages • some data • some functions to operate on that data • Remember cin.getline(…)?? • cin was an object that knew about getting “stuff” from the keyboard • getline was a function that you could use to ask cin to get some information from the keyboard

  10. String Object Member Functions • Strings are objects whose data is a sequence of characters. • You can ask a string • its size() – Number of characters in the string • its capacity() – Number of characters it can hold before it needs to resize (behind the scenes).

  11. http://www.msoe.edu/eecs/ce/courseinfo/stl/string.htm

  12. Questions??? ?

More Related