06.08.2013 Views

Laboratory Exercises, C++ Programming

Laboratory Exercises, C++ Programming

Laboratory Exercises, C++ Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Programming</strong> with the STL 29<br />

4 <strong>Programming</strong> with the STL<br />

Objective: to practice using the STL container classes and algoritms, with emphasis on efficiency.<br />

You will also learn more about operator overloading and STL iterators.<br />

Read:<br />

• Book: STL containers and STL algorithms. Operator overloading, iterators.<br />

1 Domain Name Servers and the STL Container Classes<br />

On the web, computers are identified by IP addresses (32-bit numbers). Humans identify<br />

computers by symbolic names. A Domain Name Server (DNS) is a component that translates<br />

a symbolic name to the corresponding IP address. The DNS system is a very large distributed<br />

database that contains billions (or at least many millions) of IP addresses and that receives billions<br />

of lookup requests every day. Furthermore, the database is continuously updated. If you wish<br />

to know more about name servers, read the introduction at http://computer.howstuffworks.<br />

com/dns.htm.<br />

In this lab, you will implement a local DNS in <strong>C++</strong>. With “local”, we mean that the DNS does<br />

not communicate with other name servers; it can only perform translations using its own database.<br />

The goal is to develop a time-efficient name server, and you will study different implementation<br />

alternatives. You shall implement three versions (classes) of the name server, using different STL<br />

container classes. All three classes should implement the interface NameServerInterface:<br />

typedef std::string HostName;<br />

typedef unsigned int IPAddress;<br />

const IPAddress NON_EXISTING_ADDRESS = 0;<br />

class NameServerInterface {<br />

public:<br />

virtual ~NameServerInterface() {}<br />

virtual void insert(const HostName&, const IPAddress&) = 0;<br />

virtual bool remove(const HostName&) = 0;<br />

virtual IPAddress lookup(const HostName&) const = 0;<br />

};<br />

insert() inserts a name/address pair into the database, without checking if the name already<br />

exists. remove() removes a name/address pair and returns true if the name exists; it does<br />

nothing and returns false if the name doesn’t exist. lookup() returns the IP address for a<br />

specified name, or NON EXISTING ADDRESS if the name doesn’t exist.<br />

Since this is an STL lab, you shall use STL containers and algorithms as much as possible.<br />

This means, for example, that you are not allowed to use any for or while statements in your<br />

solutions. (There is one exception: you may use a for or while statement in the hash function,<br />

see assignment A1c.)<br />

A1. The definition of the class NameServerInterface is in the file nameserverinterface.h.<br />

a) Implement a class VectorNameServer that uses an unsorted vector to store the name/<br />

address pairs. Use linear search to search for an element. The intent is to demonstrate<br />

that such an implementation is inefficient.<br />

Use the STL find if algorithm to search for a host name. Consider carefully the third<br />

parameter to the algorithm: it should be a function or a functor that compares an<br />

element in the vector (a name/address pair) with a name. If you use a functor, the<br />

call may look like this:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!