Is +#+#++#5,2++++
a valid revision?
Yes. The revision +#+#++#5,2++++
starts with a +
followed by the character #
to use
as a delimiter, and extends to the next occurrence of #
. That
Add command +#+#
says to add the one character +
contained between the delimiters.
The next character +
starts another Add command.
The +
that follows it is the character to use as
a delimiter, and the text #5,2
up to the next
occurrence of the delimiter +
is the text to be
added by that Add command ++#5,2+
.
The next character +
starts another Add command. The
+
that follows it is the delimiter, and the next occurrence of
that delimiter comes right afterward. This final Add command +++
says to add no text since there is no text between the delimiters. (If would
be foolish for createRevision
to ever produce a command like
+//
or +++
or +==
since it needlessly
makes the revision string longer, but your revise
function must
still correctly handle such a command if it's in the revision string.)
+#+#++#5,2++++
has the
same effect as +/+/+/#5,2/+//
and as
+/+#5,2/
.
If createRevision
is called with an older file containing the 17
characters "abcdefghi\njklmno\n"
and a newer file containing the
15 characters cdefghi\njklmxyz"
(no final newline), would the
11 characters "#2,12+/xyz/"
be one possible correct revision
file?
Yes. Here are three examples of correct revision files for that example:
"#2,12+/xyz/"
(copy "#2,12", add "+/xyz/")
"#2,12+/xyz/\n"
(copy "#2,12", add "+/xyz/", do-nothing)
"#2,12\n+/xyz/\n"
(copy "#2,12", do-nothing, add "+/xyz/", do-nothing)
An incorrect revision file would be "#2,12+/xyz\n/"
(copy "#2,12",
incorrectly add "+/xyz\n/"); it's incorrect because the original new file did
not end with a newline, yet this revision file has an instruction that would
make a newline the last character (after the z).
How do I use std::hash
to hash a string?
If str is an expression of type string or something convertible to
string, then if you've included <functional>
, the expression
hash<string>()(str)
hashes the string and yields an
unsigned integer in the range 0 to at least 4 billion. Notice the extra set
of parentheses.
(For the insatiably curious, hash<string>()
default-constructs a temporary object of type hash<string>
,
a class that has a member function named operator()
that
takes a string and returns an unsigned integer. If t
is an
object of a class that has such a member function, then the call
t.operator()(str)
can be written
t(str)
, similarly to how for an object x
of a class that has a member function named operator[]
, like
string
and vector
, the expression
x.operator[](i)
can be written x[i]
.)
Project 4. Homework 5. What to do?
First of all, notice that Homework 5 contributes 3.8% to your total score, but should take at most an hour or two to do. With such a high return on your time investment, you'd be wise to switch your focus off Project 4 for a brief time to be sure you do Homework 5.
That said, if you haven't started on Project 4 yet, you should immediately do
so, starting by implementing only a very simple revise
function
that works only for revision files consisting of a single valid Add command
(and possibly Do-nothing commands after it); it doesn't have to work for
anything else. Such a revision need not do anything with the
fold
argument (since there are no Copy commands), and will
simply write to the fnew
argument the text between the
delimiters of the Add command.
What's the point of this? Well, we've found that any time we give an
assignment that involves reading or writing files, some people spend an
inordinate amount of time wrestling with the mechanics of opening a file.
For example, perhaps they ignored or misread the part of the
File I/O writeup that says where the file is
expected to be on a Windows or Mac or Linux machine, so they wonder why their
program can't find the file. Whatever the problem is, it's not really a C++
issue, but until it's solved it holds the person back from getting any work
done. The reason to start on the simple revise
function is to
make sure that if you have this problem, you get past it as soon as possible,
when there's plenty of office hours support, because this kind of environment
problem is most easily dealt with in person.