Data Structures代做|C++|Assignment – iterator

Data Structures代做|C++|Assignment : 这是一个数据结构代写任务,重点考察对迭代的理解

1. Overview

In this assignment, you will implement template code and not use any template classes from the standard library.You will also write your own code to handle files. Refer to the earlier assignment as to how to open and read files.

You may use the following includes,and if you think anything else is needed, post a question to Piazza: , , , , , <ioma- nip> , , , , .

Specifically,you maynot use any classes that take template parameters,suchas , ,

, , ,except for those you write yourself.Do not use shared_ptr ,and instead, explicitly manage pointers yourself using new and delete .Ensure that there are no dangling pointers and no memory leak. Use val- grind to checkonthis.

2. ProgramSpecification

The program is specified in the format of a Unix man (1) page.

NAME keyvalue manage a list of key and value pairs

SYNOPSIS keyvalue [ -@ flags ][ filename …]

DESCRIPTION Input is read from eachfile in turn. Before any processing,eachinput line is echoed to cout ,preceded by its filename and line number within the file.The name of cin is printed as a minus sign ( ). Eachnon-comment line causes some action on the part of the program, as described below.Before processing a command, leading and trailing white space is trimmed off of the key and off of the value.White space interior to the key or value is not trimmed. When a key and value pair is printed, the equiv- alent of the format string used is "%s = %s\n" .Ofcourse,use ,not .The newline character is removed from any input line before pro- cessing.Ifthere is more than one equal sign on the line,the first separates the key from the value,and the rest are part of the value.Input lines are one of the following : # Any input line whose first non-whitespace character is a hash ( # )is ignored as a comment. This means that no key can begin with a hash. An empty line or a line consisting of nothing but white space is ignored. key Aline consisting of at least one non-whitespace character and no equal sign causes the key and value pair to be printed. If not found, the mes- sage key :key not found

is printed. Note that the characters in italics are not printed exactly.
The actual key is printed. This message is printed to cout.
key =
If there is only whitespace after the equal sign, the key and value pair is
deleted from the map.
key = value
If the key is found in the map,its value field is replaced by the new
value.Ifnot found, the key and value are inserted in increasing lexico-
graphic order,sorted by key.The new key and value pair is printed.
All of the key and value pairs in the map are printed in lexicographic
= value
All of the key and value pairs with the given value are printed in lexico-
graphic order sorted by key.

OPTIONS The -@ option is followed by a sequence of flags to enable debugging output, whichiswritten to the standard error.The option flags are only meaningful to the programmer.

OPERANDS Eachoperand is the name of a file to be read. If no filenames are specified, cin is read. If filenames are specified, a filename consisting of a single minus sign ( )causes cin to be read in sequence at that position. Any file that can not be accessed causes a message in proper format to be printed to cerr.


0Noerrors were found.
1There were some problems accessing files,and error messages were
reported to cerr.

3. ImplementationSequence

In this assignment, you will constuct a program from scratch, using some of the code from previous assignments.

(a) Study the behavior of misc/pkeyvalue.perl ,whose behavior your program
should emulate.The Perl version does not support the debug option of your
(b) Copy Makefile from your previous assignment, and edit it so that it will build
and submit your new assignment.
(c) Implementyour main program whose name is main.cpp ,and handle files in
the same way asthe sample Perl code.Instead of trying to use a map,just
print debug statements showing whichofthe five kinds of statements are rec-
ognized, printing out the key and value portion of the statement.
(d) Insteadof <pair> from the standard library,you will use xpair.
(e) You will be using a linear linked list to implement your data structure.This is
obviously unacceptable in terms of a real  Data Structures problem, since unit
operations will run in O ( n )time instead of the proper O (log 2 n )time for a bal-
anced binary searchtree.But iteration over a binary searchtree is rather
complex and will not contribute to your learning about how to implement tem-
plates.And balancing a BST is part of CMPS-101, whichisnot a prerequisite
for this course.
(f) Lookat xless.h and misc/testxless.cpp ,whichshow how to create and use an
xless object to make comparisons.The listmap class assumes this has already
been declared.
(g) Thefiles *.tcc are explicit template instantiations.Templates are type-safe
macros and the source is needed at the point where they are compiled.

4. Themain function

Replace the code in the main function to do options analysis.Then, for eachinput line,use regex_search using regular expressions to parse the line into one of the three kinds of lines described above.Use the field captures to extract the key and value fields.See the example matchlines.cpp ,whichshows how to use regular expressions from the library.

5. Template classlistmap

We now examine the class listmap ,whichispartially implemented for you. Yo u need not implement functions that are never called.

(a) template <typename Key, typename Value, class Less=xless<Key>>
class listmap
defines the template class with three arguments. Key and Value are the ele-
ments to be kept in the list. Less is the class used to determine the ordering of
the list and defaults to xless<Key>.
(b) typedef Key key_type;
typedef Value mapped_type;
typedef xpair<key_type,mapped_type> value_type;
are some standard names given to usual standard library types.Note that the
value type is an xpair, not what is normally thought as the value,whichhere
is called the mapped type.
(c) struct link
represents the list itself and is contained in every node.The list is kept as a
circular doubly linked list with the list itself being the start and end, as well
as the end() result. Inalist with n nodes,there are n +1links,eachnode hav-
ing a link, and the list itself having a link, but not node values.
(d) struct node
is a private node used to hold a value type along with forward and backword
links to form a doubly linked list. It inherits from struct link .The private
function anchor() downcasts from a link to a node.
(e) listmap();
listmap (const listmap&);
listmap& operator= (const listmap&);
listmap (listmap&&);
listmap& operator= (listmap&&);
The usual six members are overriden and explicitly defined.
(f) iterator insert (const value_type&);
Note that insertion takes a pair as a single argument. If the key is already
there,the value is replaced, otherwise there is a new entry inserted into the
list. Aniterator pointing at the inserted item is returned.
(g) iterator find (const key_type&);
Searches and returns an iterator.Iffind fails,itreturns the end() iterator.
(h) iterator erase (iterator position);
The item pointed at by the argument iterator is deleted from the list. The
returned iterator points at the position immediately following that whichwas
(i) iterator begin();
iterator end();
The usual iterator generators.Wedont bother here with a constant iterator.
Since the list is circular, end() is just the list itself.

6. Template classlistmap::iterator

Although the iterator is nested inside the list map,itiseasier to read when speci- fied separately.

(a) class listmap<Key,Value,Less>::iterator
specifies precisely whichclass the iterator belongs to.
(b) friend class listmap<Key,Value>;
Only a listmap is permitted to construct a valid iterator.
(c) iterator (listmap* map, node* where);
The iterator keeps trackofboth the node and the list as a whole,sothat end()
can return an iterator off the end,
(d) value_type& operator*();
Returns a reference to some value type (key and value pair) in the list. Selec-
tions are then by dot (. ).
(e) value_type* operator->();
Returns a pointer to some value type,from whichfields can be selected with
an arrow ( -> ).
(f) iterator& operator++(); //++itor
iterator& operator--(); //--itor
Move backwards and forwards along the list. Moving off the end with ++ and
moving from an end iterator to the last element does not require special cod-
ing,since the list is a circular list. We dont bothere here with the postfix oper-
(g) void erase();
Removes the key and value pair from the list.

7. Whatto Submit

Makefile , README ,and all necessary c++ header and implementation ( *.h , *.tcc , *.cpp )files.Ifyou are using pair programming,also submit PARTNER.


电子邮件地址不会被公开。 必填项已用*标注