A (sort of) Review of Strings
- What is a string?
- Why doesn't a string show up on this picture from the book?
- Strings are an example of an Abstract Data Type.
- An abstract data type is a data type whose properties (domain and operations) are specified independently of any particular implementation.
- See page 596 of your book.
- I expect you to know this definition, and what it means.
- What is the domain of a string?
- A collection of 0 or more characters.
- Are the quotation marks part of a string?
- What are the operations on a string?
- Assignment (stringVar = "Hello World";)
- Comparison (<, <=, ==, > , >=)
- Concatenation (stringVar1 + stringVar2)
- Input/output ( cout << stringVar)
- getline(stream, string) is a utility function.
- documentation
- Normally looks for a newline character (\n),
- But can look for any character.
- Let's write a program that reads in a file one line at a time and prints out that line.
- For now compile with the following flags
- -g -O3 -std=c++14 -Wall -Wextra -Wuninitialized -pedantic -Wshadow -Weffc++ -Wunused
- c_str()
- This returns a c style string.
- More on these later, but needed for system type calls (opening a file)
- size(), length(): note these return a size_t
- Add to the program the ability to find the shortest and longest lines.
- find()
- This takes a string or character,
- And possibly a starting position.
- Take a look at cplusplus.com.
- returns a size_t, possibly the constant string::npos
- Note in this case, the find function is overloaded
- There are multiple different versions with the same name.
- The compiler decides which to use based on the parameters.
- Or the function signature
- Note also, some of the parameters have default values.
- If the user does not provide the starting position, it is assigned a value of 0.
- It turns out there are multiple find functions, but more on that later.
- Let's look for the word "Captain" in the lines of the poem.
- substr()
- Worth looking at the reference here as well.
- Note that first argument must be less than or equal to the string length.
- The first position in a string is 0.
- The second argument can be very large or even string::npos, only the end of the string is returned.
- Note string::npos is the default value of the length parameter.
- Let's split the lines on commas.
- at() and []
- Returns or changes the character at a given position.
- Valid positions are 0 through size()-1
- at will throw an exception, whatever that is, if the position is bad.
- [] will not throw an exception
- Let's remove all space at the beginning of a line.
- Let's convert entire lines to upper case.
- Let's print out only the punctuation in a line.
- Let's reverse a line.
- Actually, we probably should find all of the operations (or functions)
- Look here
- This isn't everything, the standard keeps changing.
- I wouldn't expect you to know everything here.
- But I would expect you to that this exists.
- Some other useful functions.
- The C library contains a set of character classification functions.
- isalpha, is alphanum, ...
- man isalpha
- Know that these exist.
- The C library also has toupper and tolower
- man toupper
- These convert a character only.
- AND only convert alphabet characters, others are unchanged.
- Page 123 of your book
- C library has atoi, atof, ...
- Convert a string to an int, float, long, ...
- man atoi
- The c++ library contains some conversion functions as well
- to_string()
- This is c++ 11
- Takes a numeric type and returns a string.
- Formats are limited.
- sprintf from C is an alternative which provides better formatting choices
- But that is beyond the discussion for now.
- stoi
- Part of a family sto*
- i - integer
- l - long int
- ul - unsigned long
- f - float
- d - double
- ld - long dobule
- Again it is c++11