Up till now, a program couldn't store -- couldn't remember -- anything except in variables. This meant that the program couldn't remember more data than the number of variables than the programmer put into the program. Essentially the program can't remember more data than the programmer typed in while creating the program.
Arrays shatter that limitation: An array is a single variable with which a program can store and retrieve a nearly limitless amount of data.
For example, a typical run of the program might look like:
~/$ ./prog William Shakespeare Shakespeare, William... where the red text is user input. This should be no problem, something like this will do:
string first, last;
cin >> first >> last;
cout << last << ", " << first << endl;
No matter how hard you work with what we've learned of C++, there's no way you can write this program! You can capitalize, you can switch first and last names, you just can't do both together.
The problem is:
first
and
last
.length()
at the end of it and the resulting expression returns the length of the string.
So, from the previous code I could ask for
first.length()
and (with first name "Chris") the result would be 5.
first
, for
example, has index 0. To reference it, say for printing, you write
first[0]
, which we usually read as "first zub zero".
C | h | r | i | s |
0 | 1 | 2 | 3 | 4 |
Using indices, we can make sure that every character within the string
first
is printed in capitals:
|
|
As you seen in the above code, the function cap()
capitalizes the input character.
When we access an ordered collection of objects, all of the same type, by indices rather than by individual names, we are using arrays. This is one of the most powerful ideas in computer science, so you better learn to love it!
The exercises below and the remainder of the notes here are just to help you
play with the idea of accessing objects within an array by index
rather than by specific name. A string is, essentially, and array of
objects of type char
. In coming lectures, you'll see that
we can have arrays of any type of object we choose.
Arrays allow us to store and compute with large amounts of information in programs without needing separate variable names for each object we store.
first
stood for as many char
objects as
needed.
The key behind this indexing with strings, is that the expression
first[i]
is an object of type char
. (In
particular, it is an lvalue of type char, meaning
that it can, among other things, appear on the left-hand side of
an assignment.)
You don't need any special rules to tell you
what you can and can't do with an expression like first[i]
,
because anything you can do with a char
you can do
with it.
string s;
cin >> s;
for(int i = 0; i < s.length(); i++)
s[i] = cap(s[i]); // simple assignment!
cout << s << endl;
You say "How do I know I can assign a value to s[i]
like that?" Well, s[i]
is an lvalue of
type char
like
any other so, you can assign to it just like you assign to any
char
variable!
In fact, you can create a function capitalize()
to capitalize
a string, by using the above function cap()
for characters.
// prototype
// for the string type!
string capitalize(string w);
// definition
string capitalize(string w)
{
for(int i = 0; i < w.length(); i++)
w[i] = cap(w[i]); // use cap(char ) defined above!
return w;
}
Now, given the two capitalize()
functions, printing out a
capitalized string becomes very easy.
string s;
cin >> s;
cout << capitalize(s) << endl;
string
s
and a
char c
, and tests whether or not c
appears in s
.