SlideShare a Scribd company logo
 
Chapter  8 Strings and Vectors  Copyright © 2008 Pearson Addison-Wesley.  All rights reserved.
Overview 8.1  An Array Type for Strings  8.2  The Standard  string Class 8.3  Vectors Slide 8-
8.1 An Array Type for Strings Copyright © 2008 Pearson Addison-Wesley.  All rights reserved.
An Array Type for Strings C-strings can be used to represent strings of  characters C-strings are stored as arrays of characters C-strings use the null character '\0' to end a string The Null character is a single character To declare a C-string variable, declare an array of characters:   char s[11]; Slide 8-
Declaring a C-string as char s[10] creates space for only nine characters The null character terminator requires one space A C-string variable does not need a size variable The null character immediately follows the last character of the string Example:   C-string Details Slide 8-  s[0] s[1] s[2] s[3] s[4] s[5] s[6] s[7] s[8] s[9] H i M o m ! \0 ? ?
C-string Declaration To declare a C-string variable, use the syntax: char Array_name[ Maximum_C_String_Size + 1]; + 1 reserves the additional character needed by '\0' Slide 8-
Initializing a C-string To initialize a C-string during declaration:   char my_message[20] = "Hi there."; The null character '\0' is added for you Another alternative:   char short_string[ ] = "abc";  but not this:   char short_string[ ] = {'a', 'b', 'c'};  Slide 8-
C-string error This attempt to initialize a C-string does not  cause the \0 to be inserted in the array char short_string[ ] = {'a', 'b', 'c'}; Slide 8-
Don't Change '\0' Do not to replace the null character when manipulating indexed variables in a C-string If the null character is lost, the array cannot act  like a C-string Example:  int index = 0;     while (our_string[index] != '\0')   {   our_string[index] = 'X';   index++;   } This code depends on finding the null character! Slide 8-
Safer Processing of C-strings The loop on the previous slide depended on  finding the '\0' character It would be wiser to use this version in case the  '\0' character had been removed   int index = 0;     while (our_string[index] != '\0'   &&  index < SIZE)   {   our_string[index] = 'X';   index++;   } Slide 8-
Assignment With C-strings This statement is illegal:   a_string = &quot;Hello&quot;; This is an assignment statement, not an initialization The assignment operator does not work with  C-strings Slide 8-
Assignment of C-strings A common method to assign a value to a  C-string variable is to use strcpy, defined in  the cstring library Example:  #include <cstring>     …   char a_string[ 11];   strcpy (a_string, &quot;Hello&quot;); Places &quot;Hello&quot; followed by the null character in  a_string Slide 8-
A Problem With strcpy strcpy can create problems if not used carefully strcpy does not check the declared length of the first argument It is possible for strcpy to write characters beyond the declared size of the array Slide 8-
A Solution for strcpy Many versions of C++ have a safer version of  strcpy named strncpy strncpy uses a third argument representing the  maximum number of characters to copy Example:  char another_string[10];   strncpy(another_string,      a_string_variable, 9); This code copies up to 9 characters into  another_string, leaving one space for  '\0' Slide 8-
== Alternative for C-strings The = = operator does not work as expected with C-strings The predefined function strcmp is used to compare C-string variables Example:  #include <cstring>   …   if (strcmp(c_string1, c_string2))   cout << &quot;Strings are not the same.&quot;;   else   cout << &quot;String are the same.&quot;; Slide 8-
strcmp's logic  strcmp compares the numeric codes of elements in the C-strings a character at a time If the two C-strings are the same, strcmp returns 0 0 is interpreted as false As soon as the characters do not match strcmp returns a negative value if the numeric code in the first parameter is less strcmp returns a positive value if the numeric code in the second parameter is less Non-zero values are interpreted as true Slide 8-
More C-string Functions The cstring library includes other functions strlen returns the number of characters in a string   int x = strlen( a_string); strcat concatenates two C-strings The second argument is added to the end of the first The result is placed in the first argument Example:    char string_var[20] = &quot;The rain&quot;;   strcat(string_var, &quot;in Spain&quot;);  Now string_var contains &quot;The rainin Spain&quot; Slide 8-
strncat is a safer version of strcat A third parameter specifies a limit for the number of characters to concatenate Example: char string_var[20] = &quot;The rain&quot;;   strncat(string_var, &quot;in Spain&quot;, 11); The strncat Function Slide 8-  Display 8.1 (1) Display 8.1 (2)
C-strings as  Arguments and Parameters C-string variables are arrays C-string arguments and parameters are used just  like arrays If a function changes the value of a C-string  parameter, it is best to include a parameter for the declared size of the C-string If a function does not change the value of a  C-string parameter, the null character can detect the end of the string and no size argument is needed Slide 8-
C-string Output C-strings can be output with the insertion  operator Example:  char news[ ] = &quot;C-strings&quot;;   cout << news << &quot; Wow.&quot;    << endl; Slide 8-
C-string Input The extraction operator  >> can fill a C-string  Whitespace ends reading of data  Example:  char a[80], b[80];   cout << &quot;Enter input: &quot; << endl;   cin >> a  >> b;   cout << a << b << &quot;End of Output&quot;; could produce:     Enter input:   Do be do to you!   DobeEnd of Output Slide 8-
Reading an Entire Line Predefined member function getline can read an entire line, including spaces getline is a member of all input streams getline has two arguments The first is a C-string variable to receive input The second is an integer, usually the size of the first  argument specifying the maximum number of elements in the first argument getline is allowed to fill Slide 8-
Using getline The following code is used to read an entire line including spaces into a single C-string variable char a[80]; cout << &quot;Enter input:\n&quot;; cin.getline(a, 80); cout << a << End Of Output\n&quot;; and could produce:   Enter some input:   Do be do to you!   Do be do to you!End of Output Slide 8-
getline wrap up getline stops reading when the number of  characters, less one, specified in the second argument have been placed in the C-string one character is reserved for the null character getline stops even if the end of the line has not  been reached Slide 8-
getline and Files C-string input and output work the same way  with file streams Replace cin with the name of an input-file stream     in_stream >> c_string;   in_stream.getline(c_string, 80); Replace cout with the name of an output-file stream   out_stream << c_string;   Slide 8-
getline syntax Syntax for using getline is    cin.getline(String_Var, Max_Characters + 1); cin can be replaced by any input stream Max_Characters + 1 reserves one element for the null character Slide 8-
C-String to Numbers &quot;1234&quot; is a string of characters 1234 is a number When doing numeric input, it is useful to read  input as a string of characters, then convert  the string to a number Reading money may involve a dollar sign  Reading percentages may involve a percent sign Slide 8-
C-strings to Integers To read an integer as characters Read input as characters into a C-string, removing unwanted characters Use the predefined function atoi to convert the  C-string to an int value Example:  atoi(&quot;1234&quot;)  returns the integer 1234     atoi(&quot;#123&quot;) returns 0 because # is not   a digit Slide 8-
C-string to long Larger integers can be converted using the  predefined function  atol atol returns a value of type long Slide 8-
C-string to double C-strings can be converted to type double using the predefined function atof atof  returns a value of type double Example:  atof(&quot;9.99&quot;)  returns 9.99   atof(&quot;$9.99&quot;)  returns 0.0 because the    $ is not a digit Slide 8-
Library cstdlib The conversion functions    atoi   atol atof are found in the library cstdlib To use the functions use the include directive   #include <cstdlib> Slide 8-
We now know how to convert C-strings to  numbers How do we read the input? Function read_and_clean, in Display 8.2… Reads a line of input Discards all characters other than the digits '0' through '9' Uses atoi to convert the &quot;cleaned-up&quot; C-string to int Numeric Input Slide 8-  Display 8.2 (1) Display 8.2 (2)
Function get_int, from Display 8.3… Uses read_and_clean to read the user's input Allows the user to reenter the input until the user is satisfied with the number computed from the input string Confirming Input Slide 8-  Display 8.3 (1) Display 8.3 (2)
Section 8.1 Conclusion Can you Describe the benefits of reading numeric data as characters before converting the characters to a number? Write code to do input and output with  C-strings? Use the atoi, atol, and atof functions? Identify the character that ends a C-string? Slide 8-
8.2 The Standard  string  Class Copyright © 2008 Pearson Addison-Wesley.  All rights reserved.
The Standard string Class The string class allows the programmer to treat strings as a basic data type No need to deal with the implementation as with C-strings The string class is defined in the string library and the names are in the standard namespace To use the string class you need these lines:    #include <string>     using namespace std; Slide 8-
Assignment of  Strings Variables of type string can be assigned with the = operator Example:  string s1, s2, s3;   …     s3 = s2; Quoted strings are type cast to type string Example:    string s1 = &quot;Hello Mom!&quot;; Slide 8-
Using + With strings Variables of type string can be concatenated  with the + operator Example:    string s1, s2, s3;   …     s3 = s1 + s2; If s3 is not large enough to contain s1 + s2, more space is allocated Slide 8-
string Constructors The default string constructor initializes the  string to the empty string Another string constructor takes a C-string  argument Example:    string phrase;  // empty string     string noun(&quot;ants&quot;); // a string version     //  of &quot;ants&quot; Slide 8-
It is natural to work with strings in the following manner  string phrase = &quot;I love&quot; + adjective + &quot; &quot;   + noun + &quot;!&quot;; It is not so easy for C++!  It must either convert the null-terminated C-strings, such as &quot;I love&quot;,  to strings, or it must use an overloaded + operator that works  with strings and C-strings Mixing strings and C-strings Slide 8-  Display 8.4
I/O With Class string The insertion operator << is used to output  objects of type string Example:  string s = &quot;Hello Mom!&quot;;   cout << s; The extraction operator >> can be used to input  data for objects of type string Example:   string s1;     cin >> s1;  >> skips whitespace and stops on encountering more  whitespace Slide 8-
getline and Type string A getline function exists to read entire lines into a string variable This version of getline is not a member of the  istream class, it is a non-member function Syntax for using this getline is different than that used with cin:  cin.getline(…) Syntax for using getline with string objects:   getline(Istream_Object, String_Object); Slide 8-
getline Example This code demonstrates the use of getline with string objects string line; cout &quot;Enter a line of input:\n&quot;; getline(cin, line); cout << line << &quot;END OF OUTPUT\n&quot;; Output could be:   Enter some input:   Do be do to you!   Do be do to you!END OF OUTPUT Slide 8-
The extraction operator cannot be used to read a blank character To read one character at a time remember to  use cin.get cin.get reads values of type char, not type string The use of getline, and cin.get for string input are  demonstrated in  Character Input With strings Slide 8-  Display 8.5 (1) Display 8.5 (2)
Another Version of getline The versions of getline we have seen, stop  reading at the end of line marker '\n' getline can stop reading at a character specified  in the argument list This code stops reading when a '?' is read      string line;   cout <<&quot;Enter some input: \n&quot;;     getline(cin, line, '?'); Slide 8-
getline returns a reference to its first argument This code will read in a line of text into s1 and  a string of non-whitespace characters into s2:   string s1, s2;   getline(cin, s1) >> s2;   cin >> s2; getline Returns a Reference Slide 8-  returns
getline Declarations These are the declarations of the versions of  getline for string objects we have seen istream& getline(istream& ins,  string& str_var,   char delimiter); istream& getline(istream& ins,  string& str_var); Slide 8-
Mixing cin >> and getline Recall cin >> n skips whitespace to find what it  is to read then stops reading when whitespace  is found cin >> leaves the '\n' character in the input stream Example:  int n;     string line;     cin >> n;   getline(cin, line); leaves the '\n' which immediately ends getline's  reading…line is set equal to the empty string Slide 8-
ignore  ignore is a member of the istream class ignore can be used to read and discard all the  characters, including '\n' that remain in a line Ignore takes two arguments First, the maximum number of characters to discard Second, the character that stops reading and discarding Example:  cin.ignore(1000, '\n');   reads up to 1000 characters or to '\n'   Slide 8-
The string class allows the same operations we  used with C-strings…and more Characters in a string object can be accessed as if they are in an array last_name[i]  provides access to a single character as in an array Index values are not checked for validity! String Processing Slide 8-  Display 8.6
Member Function length The string class member function length returns  the number of characters in the string object: Example:   int n = string_var.length( ); Slide 8-
at is an alternative to using [ ]'s to access  characters in a string. at checks for valid index values Example:  string str(&quot;Mary&quot;);   cout << str[6] << endl;   cout << str.at(6) << endl;   str[2] = 'X';   str.at(2) = 'X'; Member Function at Slide 8-  Other string class functions are found in  Display 8.7 Equivalent Equivalent
Comparison of strings Comparison operators work with string objects Objects are compared using lexicographic order (Alphabetical ordering using the order of symbols in the ASCII character set.) = =  returns true if two string objects contain the same characters in the same order Remember strcmp for C-strings? <, >, <=, >= can be used to compare string  objects Slide 8-
Program Example: Palindrome Testing A palindrome is a string that reads the same  from front to back as it does from back to front This program ignores spaces and punctuation Upper and lowercase versions of letters are  considered the same letter Examples:  Able was I 'ere I saw Elba.   Madam, I'm Adam.   A man, a plan, a canal, Panama.   Racecar Slide 8-
Palindrome Testing: remove_punct remove_punct removes punctuation from a string remove_punct compares each character in the string to the characters in a string containing all the punctuation characters and the space character.  If a match is not found, the character is added to the string no_punct no_punct, the original string less any punctuation or spaces, is returned Slide 8-
Palindrome Testing: substr The substr member function is used to locate  a substring within a string remove_punct uses substr to extract a single character at a time from the source string.  The  character is stored in a_char. remove_punct then uses function find to see if the character in a_char is in the string of punctuation characters Slide 8-
The entire palindrome testing program is  found in  Palindrome Testing: The Program Slide 8-  Display 8.8 (1) Display 8.8 (2) Display 8.8 (4) Display 8.8 (3)
string Objects to C-strings Recall the automatic conversion from C-string to string:  char a_c_string[] = &quot;C-string&quot;;   string_variable = a_c_string; strings are not converted to C-strings Both of these statements are illegal: a_c_string = string_variable; strcpy(a_c_string, string_variable); Slide 8-
Converting strings to C-strings The string class member function c_str returns the C-string version of a string object Example:  strcpy(a_c_string, string_variable.c_str( ) );  This line is still illegal   a_c_string = string_variable.c_str( ) ; Recall that operator = does not work with C-strings Slide 8-
Section 8.2 Conclusion Can you Show how a string object can be used like a  C-string? Write code to read an entire line into a string object? Use the string function at to access individual  characters in a string object? Write code to convert a string to a C-string? Slide 8-
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley.  All rights reserved.
Vectors Vectors are like arrays that can change size as your program runs Vectors, like arrays, have a base type To declare an empty vector with base type int:   vector<int> v; <int> identifies vector as a template class  You can use any base type in a template class:   vector<string> v; Slide 8-
Accessing vector Elements Vectors elements are indexed starting with 0 [ ]'s are used to read or change the value of an item:     v[i] = 42;   cout << v[i]; [ ]'s cannot be used to initialize a vector element Slide 8-
Initializing vector Elements Elements are added to a vector using the  member function push_back push_back adds an element in the next available position Example:  vector<double> sample;     sample.push_back(0.0);     sample.push_back(1.1);     sample.push_back(2.2); Slide 8-
The size Of A vector The member function size returns the number  of elements in a vector Example:  To print each element of a vector given the previous vector initialization:   for (int i= 0; i < sample.size( ); i++)   cout << sample[i] << endl; Slide 8-
The Type unsigned int The vector class member function size returns  an unsigned int  Unsigned int's are nonnegative integers Some compilers will give a warning if the previous for-loop is not changed to:   for (unsigned int i= 0; i < sample.size( ); i++)   cout << sample[i] << endl; Slide 8-
Alternate vector Initialization A vector constructor exists that takes an  integer argument and initializes that number of  elements  Example:  vector<int> v(10);   initializes the first 10 elements to 0   v.size( ) would return 10 [ ]'s can now be used to assign elements 0  through 9 push_back is used to assign elements greater than  9 Slide 8-
Vector Initialization  With Classes The vector constructor with an integer argument Initializes  elements of number types  to zero Initializes elements of class types using the  default constructor for the class Slide 8-
To use the vector class Include the vector library   #include <vector> Vector names are placed in the standard namespace so the usual using directive is needed:   using namespace std; The vector Library Slide 8-  Display 8.9
vector Issues Attempting to use [ ] to set a value beyond the  size of a vector may not generate an error The program will probably misbehave The assignment operator with vectors does an  element by element copy of the right hand vector For class types, the assignment operator must make independent copies Slide 8-
vector Efficiency A vector's capacity is the number of elements allocated in memory Accessible using the capacity( ) member function Size is the number of elements initialized When a vector runs out of space, the capacity is automatically increased A common scheme is to double the size of a vector More efficient than allocating smaller chunks of memory Slide 8-
Controlling vector Capacity When efficiency is an issue Member function reserve can increase the capacity of a vector Example:  v.reserve(32); // at least 32 elements   v.reserve(v.size( ) + 10);  // at least 10 more resize can be used to shrink a vector Example:    v.resize(24);    //elements beyond 24  are lost Slide 8-
Section 8.3 Conclusion Can you Declare and initialize a vector of 10 doubles? Write code to increase the size of a vector in at  least two different ways? Describe the difference between a vector's size and its capacity? Slide 8-
Chapter 8 -- End Slide 8-
Display 8.1 (1/2) Slide 8-  Back Next
Display 8.1 (2/2) Slide 8-  Back Next
Display 8.2 (1/2) Slide 8-  Back Next
Display 8.2 (2/2) Slide 8-  Back Next
Display 8.3 (1/3) Slide 8-  Back Next
Display 8.3  (2/3) Slide 8-  Back Next
Display 8.3 (3/3) Slide 8-  Back Next
Display 8.4 Slide 8-  Back Next
Display 8.5 (1/2) Slide 8-  Back Next
Display 8.5 (2/2) Slide 8-  Back Next
Display 8.6 Slide 8-  Back Next
Display 8.7 Slide 8-  Back Next
Display 8.8  (1/4) Slide 8-  Next Back
Display 8.8  (2/4) Slide 8-  Back Next
Display 8.8 (3/4) Slide 8-  Next Back
Display 8.8 (4/4) Slide 8-  Next Back
Display 8.9 Slide 8-  Back Next

More Related Content

PPT
Savitch ch 08
PPTX
C Programming Unit-3
PPTX
Strings
PPT
Computer Programming- Lecture 5
DOC
Assignment c programming
PPTX
CPP Programming Homework Help
DOCX
Type header file in c++ and its function
Savitch ch 08
C Programming Unit-3
Strings
Computer Programming- Lecture 5
Assignment c programming
CPP Programming Homework Help
Type header file in c++ and its function

What's hot (19)

PPTX
function, storage class and array and strings
PDF
PDF
C Programming Assignment
PPTX
CPP Homework Help
PPTX
Introduction to c programming
PPT
Advanced Programming C++
PPT
PDF
Strings in c mrs.sowmya jyothi
PPT
C++ Overview
PDF
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
PPTX
C introduction by thooyavan
PPTX
PPTX
Diploma ii cfpc u-4 function, storage class and array and strings
PPTX
Introduction to c++
DOCX
Functions in c++
PPTX
Btech i pic u-4 function, storage class and array and strings
PDF
Functions torage class and array and strings-
PDF
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
function, storage class and array and strings
C Programming Assignment
CPP Homework Help
Introduction to c programming
Advanced Programming C++
Strings in c mrs.sowmya jyothi
C++ Overview
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
C introduction by thooyavan
Diploma ii cfpc u-4 function, storage class and array and strings
Introduction to c++
Functions in c++
Btech i pic u-4 function, storage class and array and strings
Functions torage class and array and strings-
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
Ad

Viewers also liked (20)

PPT
Savitch Ch 18
PPT
Savitch ch 01
PPT
Savitch ch 16
PPT
Savitch ch 04
PPT
Savitch Ch 03
PPT
Savitch Ch 14
PPT
Savitch Ch 17
PPT
Savitch ch 022
PPT
Savitch Ch 02
PPT
Savitch Ch 06
PPT
Savitch Ch 01
PPT
Savitch Ch 13
PPT
Savitch Ch 11
PPT
Savitch c++ ppt figs ch1
PPT
Savitch Ch 15
PPT
Savitch Ch 12
PPT
Savitch Ch 07
PPT
Savitch Ch 10
PPT
Savitch Ch 04
PPT
Savitch Ch 05
Savitch Ch 18
Savitch ch 01
Savitch ch 16
Savitch ch 04
Savitch Ch 03
Savitch Ch 14
Savitch Ch 17
Savitch ch 022
Savitch Ch 02
Savitch Ch 06
Savitch Ch 01
Savitch Ch 13
Savitch Ch 11
Savitch c++ ppt figs ch1
Savitch Ch 15
Savitch Ch 12
Savitch Ch 07
Savitch Ch 10
Savitch Ch 04
Savitch Ch 05
Ad

Similar to Savitch Ch 08 (20)

PDF
Chapter 3 - Characters and Strings - Student.pdf
PPT
sav_ch09.ppt
PPTX
Chapter 9 C++ Programming Absolute C++ Lecture Slides
PPTX
The string class
PPTX
Strings in C programming language for students
PPT
Lesson in Strings for C Programming Lessons
PPT
string function with example...................
PPTX
Cs1123 9 strings
PPT
THE FORMAT AND USAGE OF STRINGS IN C.PPT
DOC
C aptitude.2doc
DOC
Captitude 2doc-100627004318-phpapp01
PPT
Week7.ppt
PPT
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
PPT
Basics of c++
PPT
Chapterabcdefghijklmnopqrdstuvwxydanniipo
PPTX
CSE 1102 - Lecture_7 - Strings_in_C.pptx
DOCX
C interview question answer 2
PPTX
Strings in c++
Chapter 3 - Characters and Strings - Student.pdf
sav_ch09.ppt
Chapter 9 C++ Programming Absolute C++ Lecture Slides
The string class
Strings in C programming language for students
Lesson in Strings for C Programming Lessons
string function with example...................
Cs1123 9 strings
THE FORMAT AND USAGE OF STRINGS IN C.PPT
C aptitude.2doc
Captitude 2doc-100627004318-phpapp01
Week7.ppt
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
Basics of c++
Chapterabcdefghijklmnopqrdstuvwxydanniipo
CSE 1102 - Lecture_7 - Strings_in_C.pptx
C interview question answer 2
Strings in c++

More from Terry Yoast (20)

PPT
9781305078444 ppt ch12
PPT
9781305078444 ppt ch11
PPT
9781305078444 ppt ch10
PPT
9781305078444 ppt ch09
PPT
9781305078444 ppt ch08
PPT
9781305078444 ppt ch07
PPT
9781305078444 ppt ch06
PPT
9781305078444 ppt ch05
PPT
9781305078444 ppt ch04
PPT
9781305078444 ppt ch03
PPT
9781305078444 ppt ch02
PPT
9781305078444 ppt ch01
PPTX
9781337102087 ppt ch13
PPTX
9781337102087 ppt ch18
PPTX
9781337102087 ppt ch17
PPTX
9781337102087 ppt ch16
PPTX
9781337102087 ppt ch15
PPTX
9781337102087 ppt ch14
PPTX
9781337102087 ppt ch12
PPTX
9781337102087 ppt ch11
9781305078444 ppt ch12
9781305078444 ppt ch11
9781305078444 ppt ch10
9781305078444 ppt ch09
9781305078444 ppt ch08
9781305078444 ppt ch07
9781305078444 ppt ch06
9781305078444 ppt ch05
9781305078444 ppt ch04
9781305078444 ppt ch03
9781305078444 ppt ch02
9781305078444 ppt ch01
9781337102087 ppt ch13
9781337102087 ppt ch18
9781337102087 ppt ch17
9781337102087 ppt ch16
9781337102087 ppt ch15
9781337102087 ppt ch14
9781337102087 ppt ch12
9781337102087 ppt ch11

Recently uploaded (20)

PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Insiders guide to clinical Medicine.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Insiders guide to clinical Medicine.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Week 4 Term 3 Study Techniques revisited.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
2.FourierTransform-ShortQuestionswithAnswers.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Renaissance Architecture: A Journey from Faith to Humanism
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPH.pptx obstetrics and gynecology in nursing
Complications of Minimal Access Surgery at WLH
Pharmacology of Heart Failure /Pharmacotherapy of CHF
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Anesthesia in Laparoscopic Surgery in India
FourierSeries-QuestionsWithAnswers(Part-A).pdf

Savitch Ch 08

  • 1.  
  • 2. Chapter 8 Strings and Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
  • 3. Overview 8.1 An Array Type for Strings 8.2 The Standard string Class 8.3 Vectors Slide 8-
  • 4. 8.1 An Array Type for Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
  • 5. An Array Type for Strings C-strings can be used to represent strings of characters C-strings are stored as arrays of characters C-strings use the null character '\0' to end a string The Null character is a single character To declare a C-string variable, declare an array of characters: char s[11]; Slide 8-
  • 6. Declaring a C-string as char s[10] creates space for only nine characters The null character terminator requires one space A C-string variable does not need a size variable The null character immediately follows the last character of the string Example: C-string Details Slide 8- s[0] s[1] s[2] s[3] s[4] s[5] s[6] s[7] s[8] s[9] H i M o m ! \0 ? ?
  • 7. C-string Declaration To declare a C-string variable, use the syntax: char Array_name[ Maximum_C_String_Size + 1]; + 1 reserves the additional character needed by '\0' Slide 8-
  • 8. Initializing a C-string To initialize a C-string during declaration: char my_message[20] = &quot;Hi there.&quot;; The null character '\0' is added for you Another alternative: char short_string[ ] = &quot;abc&quot;; but not this: char short_string[ ] = {'a', 'b', 'c'}; Slide 8-
  • 9. C-string error This attempt to initialize a C-string does not cause the \0 to be inserted in the array char short_string[ ] = {'a', 'b', 'c'}; Slide 8-
  • 10. Don't Change '\0' Do not to replace the null character when manipulating indexed variables in a C-string If the null character is lost, the array cannot act like a C-string Example: int index = 0; while (our_string[index] != '\0') { our_string[index] = 'X'; index++; } This code depends on finding the null character! Slide 8-
  • 11. Safer Processing of C-strings The loop on the previous slide depended on finding the '\0' character It would be wiser to use this version in case the '\0' character had been removed int index = 0; while (our_string[index] != '\0' && index < SIZE) { our_string[index] = 'X'; index++; } Slide 8-
  • 12. Assignment With C-strings This statement is illegal: a_string = &quot;Hello&quot;; This is an assignment statement, not an initialization The assignment operator does not work with C-strings Slide 8-
  • 13. Assignment of C-strings A common method to assign a value to a C-string variable is to use strcpy, defined in the cstring library Example: #include <cstring> … char a_string[ 11]; strcpy (a_string, &quot;Hello&quot;); Places &quot;Hello&quot; followed by the null character in a_string Slide 8-
  • 14. A Problem With strcpy strcpy can create problems if not used carefully strcpy does not check the declared length of the first argument It is possible for strcpy to write characters beyond the declared size of the array Slide 8-
  • 15. A Solution for strcpy Many versions of C++ have a safer version of strcpy named strncpy strncpy uses a third argument representing the maximum number of characters to copy Example: char another_string[10]; strncpy(another_string, a_string_variable, 9); This code copies up to 9 characters into another_string, leaving one space for '\0' Slide 8-
  • 16. == Alternative for C-strings The = = operator does not work as expected with C-strings The predefined function strcmp is used to compare C-string variables Example: #include <cstring> … if (strcmp(c_string1, c_string2)) cout << &quot;Strings are not the same.&quot;; else cout << &quot;String are the same.&quot;; Slide 8-
  • 17. strcmp's logic strcmp compares the numeric codes of elements in the C-strings a character at a time If the two C-strings are the same, strcmp returns 0 0 is interpreted as false As soon as the characters do not match strcmp returns a negative value if the numeric code in the first parameter is less strcmp returns a positive value if the numeric code in the second parameter is less Non-zero values are interpreted as true Slide 8-
  • 18. More C-string Functions The cstring library includes other functions strlen returns the number of characters in a string int x = strlen( a_string); strcat concatenates two C-strings The second argument is added to the end of the first The result is placed in the first argument Example: char string_var[20] = &quot;The rain&quot;; strcat(string_var, &quot;in Spain&quot;); Now string_var contains &quot;The rainin Spain&quot; Slide 8-
  • 19. strncat is a safer version of strcat A third parameter specifies a limit for the number of characters to concatenate Example: char string_var[20] = &quot;The rain&quot;; strncat(string_var, &quot;in Spain&quot;, 11); The strncat Function Slide 8- Display 8.1 (1) Display 8.1 (2)
  • 20. C-strings as Arguments and Parameters C-string variables are arrays C-string arguments and parameters are used just like arrays If a function changes the value of a C-string parameter, it is best to include a parameter for the declared size of the C-string If a function does not change the value of a C-string parameter, the null character can detect the end of the string and no size argument is needed Slide 8-
  • 21. C-string Output C-strings can be output with the insertion operator Example: char news[ ] = &quot;C-strings&quot;; cout << news << &quot; Wow.&quot; << endl; Slide 8-
  • 22. C-string Input The extraction operator >> can fill a C-string Whitespace ends reading of data Example: char a[80], b[80]; cout << &quot;Enter input: &quot; << endl; cin >> a >> b; cout << a << b << &quot;End of Output&quot;; could produce: Enter input: Do be do to you! DobeEnd of Output Slide 8-
  • 23. Reading an Entire Line Predefined member function getline can read an entire line, including spaces getline is a member of all input streams getline has two arguments The first is a C-string variable to receive input The second is an integer, usually the size of the first argument specifying the maximum number of elements in the first argument getline is allowed to fill Slide 8-
  • 24. Using getline The following code is used to read an entire line including spaces into a single C-string variable char a[80]; cout << &quot;Enter input:\n&quot;; cin.getline(a, 80); cout << a << End Of Output\n&quot;; and could produce: Enter some input: Do be do to you! Do be do to you!End of Output Slide 8-
  • 25. getline wrap up getline stops reading when the number of characters, less one, specified in the second argument have been placed in the C-string one character is reserved for the null character getline stops even if the end of the line has not been reached Slide 8-
  • 26. getline and Files C-string input and output work the same way with file streams Replace cin with the name of an input-file stream in_stream >> c_string; in_stream.getline(c_string, 80); Replace cout with the name of an output-file stream out_stream << c_string; Slide 8-
  • 27. getline syntax Syntax for using getline is cin.getline(String_Var, Max_Characters + 1); cin can be replaced by any input stream Max_Characters + 1 reserves one element for the null character Slide 8-
  • 28. C-String to Numbers &quot;1234&quot; is a string of characters 1234 is a number When doing numeric input, it is useful to read input as a string of characters, then convert the string to a number Reading money may involve a dollar sign Reading percentages may involve a percent sign Slide 8-
  • 29. C-strings to Integers To read an integer as characters Read input as characters into a C-string, removing unwanted characters Use the predefined function atoi to convert the C-string to an int value Example: atoi(&quot;1234&quot;) returns the integer 1234 atoi(&quot;#123&quot;) returns 0 because # is not a digit Slide 8-
  • 30. C-string to long Larger integers can be converted using the predefined function atol atol returns a value of type long Slide 8-
  • 31. C-string to double C-strings can be converted to type double using the predefined function atof atof returns a value of type double Example: atof(&quot;9.99&quot;) returns 9.99 atof(&quot;$9.99&quot;) returns 0.0 because the $ is not a digit Slide 8-
  • 32. Library cstdlib The conversion functions atoi atol atof are found in the library cstdlib To use the functions use the include directive #include <cstdlib> Slide 8-
  • 33. We now know how to convert C-strings to numbers How do we read the input? Function read_and_clean, in Display 8.2… Reads a line of input Discards all characters other than the digits '0' through '9' Uses atoi to convert the &quot;cleaned-up&quot; C-string to int Numeric Input Slide 8- Display 8.2 (1) Display 8.2 (2)
  • 34. Function get_int, from Display 8.3… Uses read_and_clean to read the user's input Allows the user to reenter the input until the user is satisfied with the number computed from the input string Confirming Input Slide 8- Display 8.3 (1) Display 8.3 (2)
  • 35. Section 8.1 Conclusion Can you Describe the benefits of reading numeric data as characters before converting the characters to a number? Write code to do input and output with C-strings? Use the atoi, atol, and atof functions? Identify the character that ends a C-string? Slide 8-
  • 36. 8.2 The Standard string Class Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
  • 37. The Standard string Class The string class allows the programmer to treat strings as a basic data type No need to deal with the implementation as with C-strings The string class is defined in the string library and the names are in the standard namespace To use the string class you need these lines: #include <string> using namespace std; Slide 8-
  • 38. Assignment of Strings Variables of type string can be assigned with the = operator Example: string s1, s2, s3; … s3 = s2; Quoted strings are type cast to type string Example: string s1 = &quot;Hello Mom!&quot;; Slide 8-
  • 39. Using + With strings Variables of type string can be concatenated with the + operator Example: string s1, s2, s3; … s3 = s1 + s2; If s3 is not large enough to contain s1 + s2, more space is allocated Slide 8-
  • 40. string Constructors The default string constructor initializes the string to the empty string Another string constructor takes a C-string argument Example: string phrase; // empty string string noun(&quot;ants&quot;); // a string version // of &quot;ants&quot; Slide 8-
  • 41. It is natural to work with strings in the following manner string phrase = &quot;I love&quot; + adjective + &quot; &quot; + noun + &quot;!&quot;; It is not so easy for C++! It must either convert the null-terminated C-strings, such as &quot;I love&quot;, to strings, or it must use an overloaded + operator that works with strings and C-strings Mixing strings and C-strings Slide 8- Display 8.4
  • 42. I/O With Class string The insertion operator << is used to output objects of type string Example: string s = &quot;Hello Mom!&quot;; cout << s; The extraction operator >> can be used to input data for objects of type string Example: string s1; cin >> s1; >> skips whitespace and stops on encountering more whitespace Slide 8-
  • 43. getline and Type string A getline function exists to read entire lines into a string variable This version of getline is not a member of the istream class, it is a non-member function Syntax for using this getline is different than that used with cin: cin.getline(…) Syntax for using getline with string objects: getline(Istream_Object, String_Object); Slide 8-
  • 44. getline Example This code demonstrates the use of getline with string objects string line; cout &quot;Enter a line of input:\n&quot;; getline(cin, line); cout << line << &quot;END OF OUTPUT\n&quot;; Output could be: Enter some input: Do be do to you! Do be do to you!END OF OUTPUT Slide 8-
  • 45. The extraction operator cannot be used to read a blank character To read one character at a time remember to use cin.get cin.get reads values of type char, not type string The use of getline, and cin.get for string input are demonstrated in Character Input With strings Slide 8- Display 8.5 (1) Display 8.5 (2)
  • 46. Another Version of getline The versions of getline we have seen, stop reading at the end of line marker '\n' getline can stop reading at a character specified in the argument list This code stops reading when a '?' is read string line; cout <<&quot;Enter some input: \n&quot;; getline(cin, line, '?'); Slide 8-
  • 47. getline returns a reference to its first argument This code will read in a line of text into s1 and a string of non-whitespace characters into s2: string s1, s2; getline(cin, s1) >> s2; cin >> s2; getline Returns a Reference Slide 8- returns
  • 48. getline Declarations These are the declarations of the versions of getline for string objects we have seen istream& getline(istream& ins, string& str_var, char delimiter); istream& getline(istream& ins, string& str_var); Slide 8-
  • 49. Mixing cin >> and getline Recall cin >> n skips whitespace to find what it is to read then stops reading when whitespace is found cin >> leaves the '\n' character in the input stream Example: int n; string line; cin >> n; getline(cin, line); leaves the '\n' which immediately ends getline's reading…line is set equal to the empty string Slide 8-
  • 50. ignore ignore is a member of the istream class ignore can be used to read and discard all the characters, including '\n' that remain in a line Ignore takes two arguments First, the maximum number of characters to discard Second, the character that stops reading and discarding Example: cin.ignore(1000, '\n'); reads up to 1000 characters or to '\n' Slide 8-
  • 51. The string class allows the same operations we used with C-strings…and more Characters in a string object can be accessed as if they are in an array last_name[i] provides access to a single character as in an array Index values are not checked for validity! String Processing Slide 8- Display 8.6
  • 52. Member Function length The string class member function length returns the number of characters in the string object: Example: int n = string_var.length( ); Slide 8-
  • 53. at is an alternative to using [ ]'s to access characters in a string. at checks for valid index values Example: string str(&quot;Mary&quot;); cout << str[6] << endl; cout << str.at(6) << endl; str[2] = 'X'; str.at(2) = 'X'; Member Function at Slide 8- Other string class functions are found in Display 8.7 Equivalent Equivalent
  • 54. Comparison of strings Comparison operators work with string objects Objects are compared using lexicographic order (Alphabetical ordering using the order of symbols in the ASCII character set.) = = returns true if two string objects contain the same characters in the same order Remember strcmp for C-strings? <, >, <=, >= can be used to compare string objects Slide 8-
  • 55. Program Example: Palindrome Testing A palindrome is a string that reads the same from front to back as it does from back to front This program ignores spaces and punctuation Upper and lowercase versions of letters are considered the same letter Examples: Able was I 'ere I saw Elba. Madam, I'm Adam. A man, a plan, a canal, Panama. Racecar Slide 8-
  • 56. Palindrome Testing: remove_punct remove_punct removes punctuation from a string remove_punct compares each character in the string to the characters in a string containing all the punctuation characters and the space character. If a match is not found, the character is added to the string no_punct no_punct, the original string less any punctuation or spaces, is returned Slide 8-
  • 57. Palindrome Testing: substr The substr member function is used to locate a substring within a string remove_punct uses substr to extract a single character at a time from the source string. The character is stored in a_char. remove_punct then uses function find to see if the character in a_char is in the string of punctuation characters Slide 8-
  • 58. The entire palindrome testing program is found in Palindrome Testing: The Program Slide 8- Display 8.8 (1) Display 8.8 (2) Display 8.8 (4) Display 8.8 (3)
  • 59. string Objects to C-strings Recall the automatic conversion from C-string to string: char a_c_string[] = &quot;C-string&quot;; string_variable = a_c_string; strings are not converted to C-strings Both of these statements are illegal: a_c_string = string_variable; strcpy(a_c_string, string_variable); Slide 8-
  • 60. Converting strings to C-strings The string class member function c_str returns the C-string version of a string object Example: strcpy(a_c_string, string_variable.c_str( ) ); This line is still illegal a_c_string = string_variable.c_str( ) ; Recall that operator = does not work with C-strings Slide 8-
  • 61. Section 8.2 Conclusion Can you Show how a string object can be used like a C-string? Write code to read an entire line into a string object? Use the string function at to access individual characters in a string object? Write code to convert a string to a C-string? Slide 8-
  • 62. 8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
  • 63. Vectors Vectors are like arrays that can change size as your program runs Vectors, like arrays, have a base type To declare an empty vector with base type int: vector<int> v; <int> identifies vector as a template class You can use any base type in a template class: vector<string> v; Slide 8-
  • 64. Accessing vector Elements Vectors elements are indexed starting with 0 [ ]'s are used to read or change the value of an item: v[i] = 42; cout << v[i]; [ ]'s cannot be used to initialize a vector element Slide 8-
  • 65. Initializing vector Elements Elements are added to a vector using the member function push_back push_back adds an element in the next available position Example: vector<double> sample; sample.push_back(0.0); sample.push_back(1.1); sample.push_back(2.2); Slide 8-
  • 66. The size Of A vector The member function size returns the number of elements in a vector Example: To print each element of a vector given the previous vector initialization: for (int i= 0; i < sample.size( ); i++) cout << sample[i] << endl; Slide 8-
  • 67. The Type unsigned int The vector class member function size returns an unsigned int Unsigned int's are nonnegative integers Some compilers will give a warning if the previous for-loop is not changed to: for (unsigned int i= 0; i < sample.size( ); i++) cout << sample[i] << endl; Slide 8-
  • 68. Alternate vector Initialization A vector constructor exists that takes an integer argument and initializes that number of elements Example: vector<int> v(10); initializes the first 10 elements to 0 v.size( ) would return 10 [ ]'s can now be used to assign elements 0 through 9 push_back is used to assign elements greater than 9 Slide 8-
  • 69. Vector Initialization With Classes The vector constructor with an integer argument Initializes elements of number types to zero Initializes elements of class types using the default constructor for the class Slide 8-
  • 70. To use the vector class Include the vector library #include <vector> Vector names are placed in the standard namespace so the usual using directive is needed: using namespace std; The vector Library Slide 8- Display 8.9
  • 71. vector Issues Attempting to use [ ] to set a value beyond the size of a vector may not generate an error The program will probably misbehave The assignment operator with vectors does an element by element copy of the right hand vector For class types, the assignment operator must make independent copies Slide 8-
  • 72. vector Efficiency A vector's capacity is the number of elements allocated in memory Accessible using the capacity( ) member function Size is the number of elements initialized When a vector runs out of space, the capacity is automatically increased A common scheme is to double the size of a vector More efficient than allocating smaller chunks of memory Slide 8-
  • 73. Controlling vector Capacity When efficiency is an issue Member function reserve can increase the capacity of a vector Example: v.reserve(32); // at least 32 elements v.reserve(v.size( ) + 10); // at least 10 more resize can be used to shrink a vector Example: v.resize(24); //elements beyond 24 are lost Slide 8-
  • 74. Section 8.3 Conclusion Can you Declare and initialize a vector of 10 doubles? Write code to increase the size of a vector in at least two different ways? Describe the difference between a vector's size and its capacity? Slide 8-
  • 75. Chapter 8 -- End Slide 8-
  • 76. Display 8.1 (1/2) Slide 8- Back Next
  • 77. Display 8.1 (2/2) Slide 8- Back Next
  • 78. Display 8.2 (1/2) Slide 8- Back Next
  • 79. Display 8.2 (2/2) Slide 8- Back Next
  • 80. Display 8.3 (1/3) Slide 8- Back Next
  • 81. Display 8.3 (2/3) Slide 8- Back Next
  • 82. Display 8.3 (3/3) Slide 8- Back Next
  • 83. Display 8.4 Slide 8- Back Next
  • 84. Display 8.5 (1/2) Slide 8- Back Next
  • 85. Display 8.5 (2/2) Slide 8- Back Next
  • 86. Display 8.6 Slide 8- Back Next
  • 87. Display 8.7 Slide 8- Back Next
  • 88. Display 8.8 (1/4) Slide 8- Next Back
  • 89. Display 8.8 (2/4) Slide 8- Back Next
  • 90. Display 8.8 (3/4) Slide 8- Next Back
  • 91. Display 8.8 (4/4) Slide 8- Next Back
  • 92. Display 8.9 Slide 8- Back Next