Today’s Class Class string –The basics –Assignment –Concatenation –Compare & swap –Find –Conversion to C-style char * strings –Iterators.

Slides:



Advertisements
Similar presentations
Lists: An internal look
Advertisements

LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Chapter 10.
 2006 Pearson Education, Inc. All rights reserved Class string and String Stream Processing.
Engineering Problem Solving With C++ An Object Based Approach Chapter 6 One-Dimensional Arrays.
The Standard String Class Is actually a template: –typedef basic_string string This means you can have strings of things other than chars.
計算機概論實習 How to Use string Template class basic_string String manipulation (copying, searching, etc.) typedef basic_string string; Also typedef.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
 2006 Pearson Education, Inc. All rights reserved Class string and String Stream Processing.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
1 Chapter 10 Characters, Strings, and the string class.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Chapter 6 One-Dimensional Arrays ELEC 206 Computer Tools for Electrical Engineering.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Characters, Strings, and the string class.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
Characters, Strings, And The string Class Chapter 10.
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
CSC 270 – Survey of Programming Languages
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Chapter Characters, Strings, and the string class 10.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
2008YeungNam Univ. SE Lab. 1  I n n amespace std : t ypedef basic_string string ; Type wchar_t for 16bit UNICODE. A. String near-containers  I.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
Strings: C-strings vs. Strings as Objects
C-Strings We have already seen that a C-string is a null-terminated array of characters.
Class string and String Stream Processing: A Deeper Look
Standard Template Library (STL)
Characters, C-Strings, and More About the string Class
Chapter 12: More on C-Strings and the string Class
Standard Version of Starting Out with C++, 4th Edition
Array Lists Chapter 6 Section 6.1 to 6.3
Object Oriented Programming COP3330 / CGS5409
C++ STRINGS string is part of the Standard C++ Library
Strings: C-strings vs. Strings as Objects
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
10.1 Character Testing.
String class and its objects
Class string and String Stream Processing
Engineering Problem Solving with C++, Etter
Standard Version of Starting Out with C++, 4th Edition
Std Library of C++.
Today’s Objectives 28-Jun-2006 Announcements
Instructor: Dr. Michael Geiger Spring 2019 Lecture 6: Strings
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

Today’s Class Class string –The basics –Assignment –Concatenation –Compare & swap –Find –Conversion to C-style char * strings –Iterators

Reminders Final Exam –Thursday, December 19 th, 12:00 – 2:00 pm –Covers last 1/3 of class Course grade –Will be posted Friday morning –Exams = 300 points –Assignment = 250 points –Total = 550 points

Class String Template class basic_string –String manipulation operations such as copying, searching, etc. –Template definition in namespace std typedef basic_string string; Need the following in a file –#include –using std::string; –string str;

Constructors Have several different ways of initializing a string (see constructors.txt)constructors.txt string s1("cat"); string s2 = "dog"; string s3(5, 'z'); (string "zzzzz") string s4; //default constructor string s5 = s1; //copy constructor string s6(s2); //copy constructor

Possible Errors No conversion from int or char –These will create a compiler error string error1 = 'c'; string error2( 'u' ); string error3 = 22; string error4( 8 ); –Can only assign a character in this manner s1 = 'X'; s2.at(0) = 'X'; s3[1] = 'X';

Properties Different than C-style strings –The terminating NULL (‘\0’, or 0) character not necessary –Is not a pointer Has numerous member functions –Most member functions take a starting subscript location & number of characters If number of characters is too large, max chosen Will not give STATUS_ACCESS_VIOLATION or “bus error” as in C

Basic Functions Number of characters –str.length() returns the length of a string –A string starts a subscript 0 and ends at subscript str.length() – 1 –Can also use str.size() Access individual characters –str[5] or str.at(5) at( ) provides range checking (but [ ] does not) at( ) will throw an out_of_range exception

I/O Stream extraction cin >> str; –Delimited by whitespace characters getline( cin, str); –Delimited by newline Stream insertion cout << str;

Assignment Have several different ways –(See assignment.txt)assignment.txt string str1("string"), str2, str3, str4, str5; str2 = str1; str3.assign(str1);

Assignment Can assign character-by-character –But don’t forget to resize it, as a default string has 0 length str4.resize(str1.length()); for(i=0;i<str1.length();i++) str4[i]=str1[i]; str5.resize(str1.size()); for(i=0;i<str1.size();i++) str5.at(i)=str1[i];

Concatenation Also many ways to do this str3 += str1 + "SSSSS"; //str3 = stringSSSSS str4.append(str1 + "SSSSS"); //str4 = stringSSSSS str5.append(str1, 1, 4); //str5 = stringtrin //(str1’s 1 st to 4 th elements) string str6(str1 + " and " + str2); //str6 = string and string

Comparing Can use the overloaded operators >, =, <=, ==, != –Return bool values (true = 1, false = 0) –Have to keep in mind the ASCII values for characters –For example, all capital letters have a lower value than lowercase letters –So: 'A' < 'Z' < 'a' < 'z'

Comparing & Swapping Or use compare() function - A.compare(B) –If A equals B, returns 0 –If A is greater than B, returns positive number –If A is less than B, returns negative number Use function swap() to swap two strings –A.swap(B); See compare.txt for some examplescompare.txt

Class Exercise 1 What’s the output of this program? –See exercise1.txtexercise1.txt

Characteristics (char.txt)char.txt Member functions: size(), length() –Number of characters currently stored in the string Member function: capacity() –Number of characters that can be stored without increasing the memory capacity of the string Member function: max_size() –Largest possible string that can be stored Member function: empty() –Whether the string is empty or not

Find Functions (find.txt)find.txt A.find(“string”) –Attempts to find the substring in A –If found, returns the starting location of the first matching substring –If not, returns string::pos (a constant defined in class string) A.rfind(“string”) –Same, but starts on the right and searches to the left

Convert to C-style Strings Have to take several steps (convert.txt)convert.txt string str1("This is a string."); int len = str1.length(); char *str2 = new char[ len + 1 ]; //save space for null str1.copy( str2, len, 0 ); // copy characters out of string into allocated memory str2[ len ] = 0; //add null terminator

Convert to C++-style Strings At least three ways –Use a constructor string str3(str2); –Use a for loop string str4; str4.resize(len); for(int i=0;i<len;i++) str4[i]=str2[i]; –Use an iterator

Iterators Using an iterator to traverse a string string str5; str5.resize(len); string::iterator i = str5.begin(); k = 0; while(i != str5.end()){ *i = str2[k]; i++; k++; } cout << "str5 = "<<str5<<endl;

Iterators Iterators have many features in common with pointers –Used to point to the elements in a container Container = generic (template) data structures –The dereferencing operator (*) dereferences an iterator, so you can use the element to which it points –Increment (++) and decrement (--) operators are used to move to the next element of the container

Iterators Container functions used with iterators –begin() Returns an iterator pointing to the first element of the container –end() Returns an iterator pointing to the first element past the end of the container

Iterators Have two types of iterators –An object of type iterator Refers to a container object that can be modified –An object of type const_iterator Refers to a container object that cannot be modified

Class Exercise 2 What’s the output of this program? –See exercise2.txtexercise2.txt