CIS 29 - Notes for Tuesday, 1/26

Announcements and Reminders

Recording

Comments on Assignment 2

  • Be specific on your compiler comment:  Code::Blocks 20.03, Visual Studio 2019, g++ 10.2, Xcode 12.4, Eclipse 2020-12
  • "Please resubmit" with an error in your code (crashes on execution) = -3 points

The string class

typedef basic_string<char> string;

Constructors

string();
string(const char* str);
string(const str& str);
string(const string& str, size_t pos, size_t len=npos);
string(const char* s, size_t n);
string(size_t n, char c);
template <class InputIterator> string(InputIterator first,InputIterator last);
string(initializer list<char> il);
string(string&& str);

Example 5-1 – string constructors

Iterator Functions

begin 
end
rbegin
rend
cbegin 
cend
crbegin
crend

Example 5-2 – string iterator functions

Capacity Functions

size
length
capacity
max_size
reserve
clear
resize
empty
shrink_to_fit

Example 5-3 – capacity functions

Access Functions

at
back
front

Example 5-4 – access functions

Modifier Functions

assign
append
erase
insert
push_back
replace
swap
pop_back

Example 5-5 – modifier functions

Search Functions

find
find_first_of
find_last_of
find_first_not_of
find_last_not_of
rfind

Example 5-6 – search functions

Operation Functions

c_str
compare
copy
substr

Example 5-7 – operation functions

Non-member Functions

getline

istream& getline(istream&  is, string& str, char delim);
istream& getline(istream&  is, string& str);

swap

void swap(string& x, string& y);

Example 5-8 – Non-member string functions

Member Operators

operator=
operator[]
operator+=

Non-member Operators

operator+
operator<<
operator>>

Example 5-9 – Member and non-member string operators

Member Constant

npos

npos is a static member constant, equal to the maximum value for type, size_t.  It is used to indicate the location beyond the length of a string, or with use of a find function, the return value, not found.

static const size_t npos = -1;



The stringstream classes

istringstream

Example 6-1 – Using istringstream for parsing input

Example 6-2 - A practical example

ostringstream

Example 6-3 – Using ostringstream to compose output

stringstream

Example 6-4 – Using the stringstream class