- include files
- there are three types of include files - c++ standard, c standard, and
others
- include file extensions
- the c++ and c standard include filenames do not have extensions - no
.h
- other include filenames can be arbitrary
- be careful -
<string> is not <string.h> is not <cstring>
- the
std namespace
- the c++ and c standard include files define names inside the
std
namespace
- compilers respect this to varying degrees
- the c++ standard include files
-
<iostream>, <iomanip>, <fstream> - stream io
-
<utility>, <vector>, <list>, <deque>, <queue>,
<stack>, <map>, <set>, <bitset>, <functional>,
<memory>, <iterator>, <algorithm> - stl includes
-
<exception>, <stdexcept> - exceptions
-
<string>, <sstream> - strings
-
<locale>, <limits>, <typeinfo> - odds and ends
- the c standard include files
- drop the
.h, prepend a 'c' - from <stdio.h>
to <cstdio>
-
<cassert>, <cctype>, <cfloat>, <climits>, <cmath>,
<cstdio>, <cstdlib>, <cstring>, <ctime>
- use the
c-prepended header files
- accessing the
.h forms is nonstandard
-
c-prepended headers make the proper C++ extern declarations,
.h headers do not
- any other include files are non-standard C++ files -
strcasecmp()
and strings.h and the X Windows includes
- "non-standard" is not bad - "unconventional" is bad
- don't include source (
.cc) files - but the stl does
- templates trip over this convention
- the string data type
- some terminology - the string data type and character arrays
-
string s; vs. char s[10];
- character arrays are bad
- arrays are bad - fixed size, no bounds checking
- character arrays are bad - the null byte, clumsy operations,
relations, errors are subtle
- the string data type is good
- strings are defined in the header file
<string>
- why strings are better than character arrays
- strings don't have a null byte - nothing to forget, nothing to
remember
- strings manage space themselves - no overflows, no leaking memory
- comparisons work as expected
- of course, they're slower and bigger, but so what
- string declarations and initialization
- use
#include <string> to get access to strings
- part of the standard c++ library - need the
std:: prefix
- use the
using std::string; statement to forego the
std:: prefix
- strings can be initialized in the usual way
-
std::string name = "jacaranda";
- a string with no initialization is the empty string - the string
with no characters
- strings are accessed with subscripts, just like arrays
- the index range is the same
- the
length() function returns the number of characters in a
string
- unfortunately, subscript access to strings is not checked
- the
at() accessor is checked
- string assignment and access
- assignment behaves like normal
- strings are accessed with subscripts, just like arrays
- unfortunately, subscript access to strings is not checked
-
s[s.length() + 1] = 'a' does not add 'a' to the end of s
- the
at() accessor is checked
-
+ does string catenation
- string comparison
== works as expected
- stream io operations
-
>> and << work as expected - eats white space
- the
getline() function doesn't eat white space
- strings and character arrays
- automatic (mostly) conversion from character arrays to strings
- conversion from strings to character arrays is not automatic
- the
c_str() member function
- the pointer returned is temporary
- characters are not character arrays - conversions between
a character and a string requires work -
str += 'a' wrong
- loop invariants
- straight-line programs are easy to understand (relatively)
- programs with loops are harder to understand
- what's happening while it's looping
- when does it stop looping
- the trick - find something about the loop that's always true
- easier to verify something stays true
- always true - before, during, and after
- example - search for an element in a vector
- the invariant must be true before the loop executes
- it must be true after the guard executes
- it must be true at the end of the loop body
- it will be true after the loop exits
- the combination of the invariant and negated loop guard gives what we
want
This page last modified on 21 February 2002.