Assigned Work Due -- A Question
Chapter 18 pages -- Strings
How important can strings be in C++ projects?
Vital.
You might as well add "#include <string>" to every program you start to write.
Chapter 18 pages 837-900 -- Comparing string
Chapter 18 pages 899 -- comparing strings
How would "compare" be used in a program?
Can you demonstrate comparing strings?
[ compStrings.cpp ]
Personally, I use the relational operators.... compare is a hangover from C, IMHO.
Chapter 18 pages 894 -- Strings
Can you better explain this " typedef basic_string < char > string; "
The standard library <string> defines a very general form of string. It can handle any kind of character. It uses a template for this. "basic_string<char>" creates a special class of object for the normal case -- ASCII 8bit chars. We won't be worrying about the general form in this course.
The "typedef" command creates an abbreviation:
typedef basic_string<char> string;It means that you never have to type the full description "basic_string<char>" you just type "string". Which is cool.
The "typedef" is in the <string> library so you can now forget about it until you have to work with some kind of weird characters other than 8bit ASCII.
Use typedef to abbreviate complex types
Examples
typedef vector< vector <double> > matrix;
typedef string::iterator SI;
typedef char tictactoe [3][3];
Chapter 18 pages 894 -- String Functions
Are the string member functions, like string.replace(...), c++'s way of doing
regular expressions?
No:-( You need a special library. I believe they are available in the Boost library mentioned in this text book.
Chapter 18 pages 895-901 -- concatenation
What is concatenation used for?
You use concatenation any time you want to combine two or more strings into a single. For example, to combine the parts of a person's names into a single string we would use
firstName + " " + initial + ". " + SecondNamed
Chapter 18 pages 895-897 -- String Assignment and Concatenation
How does the assign function work?
I'm note sure what happens inside the functions. What I think happens in "assignment" is that characters are copied, one at a time, from one string to another.
With concatenation a new string is created by copying characters, one at a time, after the first string.
In both cases some high energy magic is needed to handle the storage. Luckily this is hidden "under the hood" so we don't have to worry about it.
Chapter 18 pages 895 -- String initialization
Why is this an error
string error1 = 'c';and this isn't?
string1 = 'n';What is the difference between the two?
This is a nasty trap set by the syntax/grammar of C++.
The first declares and constructs a new string variable and the second changes the value of a string that you have already declared.
The "=" sign has a different meaning in declarations and assignments. Same symbol, different semantics.
Put your analyst on danger money. This gets hairy.
In a declaration like
string error1 = 'c';the compiler picks a constructor for the object whose parameters match the value after the equals sign. It converts the type if it has to.
So it changes the code to
string error1( 'c');BUT the string class does not have a
string::string (char c){....}
constructor. The nearest match is
string::string (int length){....}
SO, the compiler converts it into
string error1( (int)'c');which creates a string of 90+ characters!
By the way
string ok = "c"; //ok has one 'c' in it.works because there is a "string(char *)" constructor in class string. And so does
string ok(10, 'c'); //ok has 10 'c's in it.
Chapter 18 pages 900 -- Substrings
Can you give us an example of a program containing Class string and can you explain how it works?
You never have to write a "class string".... that is inserted for you when you write
#include <string>
Some example programs [ 14sort.cpp ] [ animal7.cpp ] [ appoint.cpp ] [ caesar.cpp ] [ compStrings.cpp ] [ conversion.cpp ] [ exprEval.cpp ] [ family.cpp ] [ Hello2001.cpp ] [ hello.cpp ] [ Hello.cpp ] [ instr.cpp ] [ jm.cpp ] [ mc3.cpp ] [ instr.h (TBD) ] [ nodup.cpp ] [ opiovect.cpp ] [ outin.cpp ] [ polystud3.cpp ] [ stringFind.cpp ] [ tmin.cpp ] [ tss2000.cpp ] [ tss2004.cpp ] [ tss2009.cpp ]
Chapter 18 pages 901 -- swap
Is the form for swap any different from strings and templates?
A template generates code for any type of data -- including string. If well written the result will compile and work. Use the standard library swap unless a teacher asks you to write your own swap.
Chapter 18 pages 901 -- swapping strings
Is it possible to swap two strings without using any third variable, and with out the using namespace std; swap?
The correct name is
std::swap
USE IT!
Swapping strings always involves some temporary data storage -- a third variable. Ask any juggler. About the best you can do is to swap one character at a time and adjust the allocated memory. DON'T DO THIS!
Chapter 19 pages 902-904 -- String characteristics
When is it necessary to use the max_size function, can you also write a simple code for this function?
Never. To be more precise..... if you hit the max_size limit the program is going to die anyway.
Chapter 18 pages 904-905 -- find function
When would a programmer need to utilize the find function and how do you use it efficiently?
Use "find" to see if a given string is inside another
if( searchThisfind(forThis) != string::npos ) ....it is there....
For example, here's a trick I used back in the 1980s, to find out if the user has typed "Yes", "yeah", "Y", "y" etc as a reply to a question:
const string positive = "YyTt";
string firstLetter=response.substr(1,1);
if( positive.find(firstLetter) != string::npos ) //yes,yes,True, true...
Use "find" when you need to know where a string is inside another string
Also -- BE CREATIVE.
Chapter 18 pages ppp-ppp -- substrings
When are substrings useful?
All the time. Any time you need to slice and dice some data -- substr will help.
Also -- BE CREATIVE.
Chapter 18 pages 902 -- String Stream Processing
What is string stream processing used for?
Be creative..... but a classic use is next:
Converting character data to numeric values and vice versa
Can you demonstrate how to convert strings to numeric values and vice versa? Is it easier to validate numeric values or string/char values? Is it better to convert everything to either all numeric or all string/char values or to have a combination of both?
Taking the last question first.
You should only convert data when necessary. It is best to leave it in the form it is in. So if you have a a character string like "909-537-5373" that represents a phone number you should NOT try converting it to three ints. The only reason for converting characters to numbers is so you can do arithmetic on them. You don't add and subtract area codes! More examples: Zip codes and personal Ids (SSNs). It is rarely worth going to the trouble of converting these to numbers.
Going from numbers (int, float, double) to characters is nearly always for output. Similarly conversions to numeric forms is on input.
In C++ the neatest techniques for converting between numbers and strings is using stringstreams in the <stream> library. See the book and my notes. Two utilities are in [ ../cs201/stringnumbers.h ] and examples in [ string.html#stringstreams ]
Validation opens a can of worms. My preferred technique is to read human generated data as characters and use stringstreams to attempt different "parsings" until one works. For more complex data I use syntax-directed designs that are not part of CS202... but mentioned in CSci320 and compiler classes.
Chapter 18 pages 912 -- string stream processing
The book states using istringstream and ostringstream for inputing and outputing string streams. Can you use stringstream for both, like using fstream for both ifstream and ofstream?
Yes. But I'd be careful to open it for one and close it before revering the flow.
Chapter 18 pages 909 -- null termination
How do string member functions know where the end of string is supposed to be marked if strings are not null terminated?
I don't know.... and more important I don't need to know .... and I don't care as long as it works.
You have to pay me to more to get inside library functions -- as long as they work!
Chapter 18 pages 911-912 -- strings and iterators
what are iterators
An iterator is a special purpose pointer that points at items in a particular container. For example, a string is a container of characters. A "vector<int>" is a container of "int"s. So a
string::iteratorselects or points out a particular character in the string.
All iterators have the operations commonly used with pointers:
Table
| Symbol | Example | Purpose |
|---|---|---|
| ++ | p++, ++p | Move to point at next item in container |
| -- | p--, --p | Move to point at previous item in container |
| * | *p | Get the item p is pointing at |
| == | p==q | Find out if p and q point at the same item |
| != | p!=q | Find out if p and q do not point at the same item |
| = | p=q | Make p point at the same item as q. |
All the standard containers (in the STL) have iterators defined for them. If T symbolizes a container (like "string") then
T::iterator p;declares an iterator that can select one item in container of type T.
If c is a container of type T then two special values are defined for
an iterator
Table
| General form | Meaning |
|---|---|
| c.begin() | The first item in the container |
| c.end() | The position immediately after the last item in c |
Example and exercise on string iterators
Complete this code that does something to each character in a string called s:
for(string_____iterator p = s.begin_______; p != ______(); p_____)
do_something_to( _______p );
Chapter 18 pages 896-897 -- String Processing
Can you walk us through Figure 18.1 in class?
If we still have time.
Exercises
Depends on the questions!
. . . . . . . . . ( end of section CSci202 Computer Science II, Session 14 Strings) <<Contents | End>>
Laboratory 7 Exceptions
[ lab07.html ]
Next -- Searching and Sorting
[ 15.html ]
Bring a pack of cards to the next class