Upload
frankjamison
View
222
Download
0
Embed Size (px)
Citation preview
8/13/2019 01c String
1/40
Dr. Ahmad R. Hadaegh
A.R. Hadaegh National Universi ty (NU ) Page 1
The C++String Class
The contents of this particular lecture prepared by the instructors at the
University of Manitoba in Canada and modified by Dr. Ahmad Reza Hadaegh
8/13/2019 01c String
2/40
8/13/2019 01c String
3/40
Dr. Ahmad R. Hadaegh
A.R. Hadaegh National Universi ty (NU ) Page 3
What is a Character String?
- This is exactly what the C++ string class does
for us - lets us declare variables as STRINGS,
and not care how they are implemented
- they are in fact not implemented in precisely this
manner in C++, but the beauty of it is, that doesn't
affect us!
- Strings are an Abstract Data Type. We will have
general operations that will do specific things
- And we COULD implement those operations
ourselves with some scheme; we just don't have to!
8/13/2019 01c String
4/40
8/13/2019 01c String
5/40
Dr. Ahmad R. Hadaegh
A.R. Hadaegh National Universi ty (NU ) Page 5
The C++ String Class
- To declare a variable of type string:
#include // declaring 3 variables of type string
string play, author, mainCharacter;
- We can assign to strings:
play = Hamlet;
author = W. Shakespeare;mainCharacter = play;// assign to string
-Notice: we dont worry about the length!
8/13/2019 01c String
6/40
Dr. Ahmad R. Hadaegh
A.R. Hadaegh National Universi ty (NU ) Page 6
String Class I/O
- Do I/O on a string in a natural way:
string name = Jim Anderson;
cout
8/13/2019 01c String
7/40Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 7
String Class I/O
- String class input:
cin >> name;// reads first word from input stream
cout > skips over whitespace characters, reads next
word (until next whitespace character)
- Notice the difference
> read in the next (whitespace delimited) word
8/13/2019 01c String
8/40
8/13/2019 01c String
9/40Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 9
String Class I/O- A simple program to copy one file to another
#include #include
int main()
{
string line;
ifstream infile; ofstream outfile;
infile.open("infile.txt");
outfile.open("outfile.txt");
getline(infile,line);
while (!infile.eof()) {
outfile
8/13/2019 01c String
10/40
8/13/2019 01c String
11/40Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 11
String Element Access
- String bounds are checked:
- access a string at an index beyond the last
character (or
8/13/2019 01c String
12/40Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 12
String Element Access
- Example of "size" function:
string name=Adam Smith";
cout
8/13/2019 01c String
13/40
8/13/2019 01c String
14/40Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 14
Objects
- An object is an abstract grouping of dataand operations
- Imagine a CAR object - all the facts about
your car- It's green, it's a 4 door, it's rusty,
- COMBINED with all the operations
- you can DRIVE my car
- you can STOP my car
- you an TURN- LEFT or TURN- RIGHT
- We will talk about how we build these later!
8/13/2019 01c String
15/40
8/13/2019 01c String
16/40Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 16
String Element Access- Example: Reversing a string
int main(){
string line;
char tmp;
int i;
cout
8/13/2019 01c String
17/40
8/13/2019 01c String
18/40Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 18
Strings as Parameters
- Strings behave "normally" when used as
parameters
- can be passed by value, or reference (with &)
- unlike C-style strings, or arrays of chars for
that matter, which are always passed by
reference
- can still use size() inside a function - the whole
object gets passed, all members are accessible
- can be returned from functions- unlike C-style strings which can't be easily
returned from functions
- Essentially, strings behave like ints when
used as parameters/return values
8/13/2019 01c String
19/40
8/13/2019 01c String
20/40Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 20
Strings as Value Parameters
- Call this function as follows:
string line1, line2;
line1= This is Adam Smith";
line2= reverse( line1);
cout
8/13/2019 01c String
21/40
8/13/2019 01c String
22/40Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 22
Strings as Reference Parameters
- Try out reverse2:
string line1, line2;
line1="This is Mr. Brown";
cout
8/13/2019 01c String
23/40
8/13/2019 01c String
24/40Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 24
String Operators: Concatenation
- Another operator provided by the string classlibrary is concatenation using +:
string word1, word2, word3, line="I ";
word1="am "; word2=an "; word3="instructor in this college" ;
line= line+ word1 + word2 + word3;
cout
8/13/2019 01c String
25/40
Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 25
String Operators: Concatenation
- This is an example of operator overloading
- operator + is used for ints, floats, and strings
- has a different meaning in each case
- operator + for strings is actually not part of C++
- it is part of the C++ class library
- you can (in the future) define your own
meanings for various operators
8/13/2019 01c String
26/40
8/13/2019 01c String
27/40
Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 27
Overloading Shortcuts
- The reason I bring this up is that += isoverloaded for strings as well
- you can write:
line += word1+ word2+ word3;
- same meaning asline = line+ word1+ word2+ word3;
- The others don't make sense for strings, but
you may see them overloaded for othertypes
- and as already stated possible to overload
them yourself
8/13/2019 01c String
28/40
Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 28
String Member Functions- The string class provides a number of useful
member functions
- Object-Oriented-Programming (OOP)
- recall that member functions of a class are
functions which operate on objects of that class
(really part of the object!)
- sometimes called "Methods- notation is:
VariableName.FunctionName(...)
- the function FunctionName operates on the
object (string) VariableName
- may have some other parameters as well
(e.g. fin.open() for file streams)
8/13/2019 01c String
29/40
Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 29
String Member Functions
- Substrings:
- the string class provides the substr memberfunction to extract substrings from a string
- Specification:
string str1, str2;str2= str1.substr( first, numchars);
- str2 is assigned the substring of str1 starting
at position first and containing numchars
characters
- if str1 does not have numchars chars after
first, str2 just copies up to end of str1
8/13/2019 01c String
30/40
Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 30
String Member Functions
- Substrings (continued)
- Example:string line="This course is not that hard guys";string line2, line3;line2 = line.substr(5,6);line3 = line.substr(15,8);cout
8/13/2019 01c String
31/40
Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 31
String Member Functions
- Substring replacement- The substr function does not modify its object
- If you want to change a substring, use the
replace function
- Specification:
string str1, str2;
str1.replace( first, numchars, str2);
- Action:- replace chars in str1, starting at index first
and extending numchars positions, with the
string str2.
8/13/2019 01c String
32/40
Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 32
String Member Functions
- Example
string line="Let us now replace some text.";
line.replace( 4, 10, "it snow. P");
cout
8/13/2019 01c String
33/40
Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 33
String Member Functions
- Can replace substrings with shorter orlonger substrings
- the length of the string will change
- Example:
string line="Let us now replace some text.";
cout
8/13/2019 01c String
34/40
8/13/2019 01c String
35/40
Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 35
String Member Functions
- Erase:
- Specification:
string line;
line.erase( start, numchars);
- delete numchars chars starting at index start
- functionally same as:
line.replace( start, numchars, "");
- "" is the null string (length 0)
8/13/2019 01c String
36/40
8/13/2019 01c String
37/40
Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 37
String Member Functions
- Examples:
string line="Walking on the sun is hot";
line.erase(15,4);
cout
8/13/2019 01c String
38/40
Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 38
String Search Functions
- The string class also offers a very useful
searching function: find
- Specification:
string line, pattern;int pos, x;
...
x = line.find( pattern, pos);
- x will contain the first location of pattern within
line starting at or after position pos or line.npos
(a special constant) if pattern not found in line
at or after position pos
8/13/2019 01c String
39/40
Dr. Ahmad R. HadaeghA.R. Hadaegh National Universi ty (NU ) Page 39
String Search Functions- Example:
string line=" This and that and more and less";int p;
p= line.find( "and" ,0); // search, start at pos'n 0
while (p != line.npos)
{
cout
8/13/2019 01c String
40/40
- Makes Sure to Study all the examples for this lecture.
- Specially, examples 10 and 11are very important.Similar questions may appear in the labs, assignments
and exams