Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

compiler required: visual studio 2019 and do not use #include (cannot use another compiler. my professor only allows this compiler) Write a class template for

compiler required: visual studio 2019 and do not use #include (cannot use another compiler. my professor only allows this compiler)

Write a class template for a map with a fixed type for the keys string. and a template type parameter for the values. Use open hashing (chaining) with an array or vector of pointers to STL linked lists. Provide the following public member functions:

  • Constructor
  • Destructor
  • Copy constructor
  • Assignment operator
  • Operator ==
  • Function size that returns the number of key-value pairs in the map
  • Function count that returns the number of elements with a specific key
  • Function insert that inserts a key-value pair into the map
  • Function at that returns a reference to the mapped value of the element identified with a specific key k. If k does not match the key of any element in the map, the function should throw an out_of_range exception.
  • Function erase that removes a key-value pair identified with a specific key k. If k does not match the key of any element in the map, the function should throw an out_of_range exception.
  • Function key_comp that returns a key comparison object, a function object comparing keys and returning true if the first argument (key) for comparison is less than the second and false otherwise

Provide a test program to test your template with at least two different types of values.

Hint: First implement a class for values of some fixed data type, for example integer or string, and then convert it to a class template. It makes sense to submit both the non-template and the template versions.

String.cpp:

#include #include using namespace std; #include "string.h"

String::String() { len = 0; buffer = NULL; // No need to allocate array to hold zero characters }

String::String(const char p[]) { // Determine number of characters in string (strlen(p)) len = 0; while (p[len] != '\0') len++; // Allocate buffer array, remember to make space for NULL character buffer = new char[len + 1]; // Copy new characters (strcpy( buffer, p )) for (int i = 0; i < len; i++) buffer[i] = p[i]; buffer[len] ='\0'; }

String::String(const String& right) { len = right.length(); buffer = new char[len + 1]; for (int i = 0; i < len; i++) buffer[i] = right[i]; buffer[len] = '\0'; }

String::~String() { delete[] buffer; }

int String::length() const { return len; }

char& String::operator [] (int index) { assert((index >= 0) && (index < len )); return buffer[index]; }

char String::operator [] (int index) const { assert((index >= 0) && (index < len )); return buffer[index]; }

String& String::operator=(const String& right) { if (this != &right) { delete[] buffer; // Get rid of old buffer len = right.length(); buffer = new char[len + 1]; for (int i = 0; i < len; i++) buffer[i] = right[i]; buffer[len] = '\0'; } return *this; }

ostream& operator << (ostream& out, const String& right) { int n = right.length(); for (int i = 0; i < n; i++) out << right[i]; return out;

}

string.h:

#ifndef STRING_H #define STRING_H class String { public: String(); // Default constructor String(const char p[]); // Simple constructor String(const String& right); // Copy constructor ~String(); // Destructor String& operator=(const String& right); // Assignment operator String& operator+=(const String& right); int length() const; char& operator[](int index); char operator[](int index) const; private: char* buffer; int len; }; #endif

stringtest.cpp:

#include #include "string.h" using namespace std; ostream& operator << (ostream& out, const String& right); void main(){ String new_name; cout << new_name << endl; char letters[] = "Hello World"; String s(letters); cout << s << endl; String first("Fred"); cout << first << endl; String second(first); cout << second << endl; String third = first; // Also uses copy constructor cout << third << endl; String classmates[5] = {"Jim", "Peter"}; for (int i = 0; i < 5; i++) cout << i << " " << classmates[i] << endl; String test = classmates[2]; cout << "test is "<< test << endl; cout << test.length() << endl; //cout << (int)test[0] << endl; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Solaris 11.2 System Administration (oracle Press)

Authors: Harry Foxwell

1st Edition

007184421X, 9780071844215

More Books

Students also viewed these Databases questions

Question

Who was the first woman prime minister of india?

Answered: 1 week ago

Question

Explain the concept of going concern value in detail.

Answered: 1 week ago

Question

Define marketing.

Answered: 1 week ago

Question

What are the traditional marketing concepts? Explain.

Answered: 1 week ago