* recall that the C++ string class has many
useful methods!
* length, which expects no arguments and returns
the number of characters in the calling string
string category = "food";
cout << category.length() << endl;
category = "beverages";
cout << category.length() << endl;
* method at - expects the desired position
within a string, and returns the character
at that position in the calling string
(0 is the position of the initial character)
cout << category.at(2) << endl; // print v
* let's use these in a function named
vertical
* classic C++ shortcuts
* these are NOT vital --
they ARE convenient
(and I do want you to be able to
read and understand them)
* consider:
int count = 0;
count = count + 1;
double sum_so_far = 0;
...inside a loop
sum_so_far = sum_so_far + next_val;
* we modify existing locations in memory
a lot;
SO.....
shorthand #1:
C++ provides the following operators:
+= -= *= /=
count += 1; is the SAME as
count = count + 1;
amount *= quantity; is the SAME as
amount = amount * quantity;
total -= discount; is the SAME as
total = total - discount;
value /= 3.0; is the SAME as
value = value / 3.0;
* ANOTHER common C++ shortcut:
* people add 1 to things SO much,
C++ has an operator JUST for that,
++
(also a -- operator for subtracting
one from something)
* these have BOTH prefix and postfix versions --
int count;
count++;
++count;
count--;
--count;
// AND they MUST be used with something
// that can be set, like a local variable...!
* do you see that:
int count = 14;
count++;
cout << count << endl; // prints 15
++count;
cout << count << endl; // prints 16
count--;
cout << count << endl; // prints 15
--count;
cout << count << endl; // prints 14
* yes, at least FOUR ways to add 1 to something!
int quant = 45;
quant = quant + 1;
quant += 1;
++quant;
quant++;
* note that ++ and -- used with an operand
IS an expression...
you CAN say:
value = 3 + (quantity++);
value = 3 + (++quantity);
* the value of (quantity++) is the value of
quantity BEFORE you add 1 to it;
* the value of (++quantity) is the value of
quantity AFTER you add 1 to it;
* EXAMPLE
int quantity;
int value;
quantity = 5;
value = 3 + (quantity++);
// after this,
// value is 8 -- 3 + quantity-before-incrementing,
// quantity is 6
quantity = 5;
value = 3 + (++quantity);
// after this,
// value is 9 -- 3 + quantity-after-incrementing,
// quantity is 6
* the key:
++blah in a BIGGER expression,
it means "increment blah THEN use its value"
blah++ in a BIGGER expression,
it means "use blah's value THEN increment it"
* let's bring in C++ ARRAYS now
* this is our kind-of-analogue to Racket lists
(but really a different DATA STRUCTURE)
* collection of things
* BUT the C++ array is a more RESTRICTED collection
of things
* all have to be of the same type
* I have to say HOW many things WHEN
I create the array
* how do I give this info to C++?
<type> <name>[<size>];
const int NUM_STUDENTS = 50;
double quiz_grades[NUM_STUDENTS];
// quiz_grades is now 50 "chunks" of memory
// each able to hold a double value
const int NUM_CITIES = 42;
string places_to_visit[NUM_CITIES];
// places_to_visit is now 42 "chunks" of
// memory each able to hold a string
...these are INDEXED, 0 onward,
and you can reference ONE element with index i
using:
places_to_visit[i]
quiz_grades[i]