Question
grades = {'jo':55, 'moe':97, 'will':74, 'bill':80)}
Here are some messages and their outcomes:
grades.keys()
['jo','moe','will','bill'}
grades.values()
[55,97,74,80]
grades.items()
[('jo',55),('moe',97),('will',74),('bill',80)]
NOTE: the above approximates python syntax, but the output format is intended to be suggestive only.
And also:
grades.add('phil',17) adds a pair to the dictionary
grades.del('moe') removes the pair ('moe',97)
grades.in('matilda') outputs true if matilda is a key in grades at this moment
grades.get('moe') should return 97
So, the idea is now to define a class in C++ that behaves like a Python dictionary, and which does the seven functions mentioned above.
The implementation should use a 'vector' to store the pairs. A pair could be a vector, or a struct, or a class. Or something else. You have tons of freedom for how to get this done. And you can display the outputs of the obvious five functions with output, using output format as you choose, as long as it seems reasonable.
It should be possible for a dictionary to be empty.
You should have 'reasonable' constructors. There are MANY ways to handle that.
You can follow the pattern that a key is a string, and a value is an int.
No two pairs in a dictionary are allowed to have the same key.
You should overload the operator + to do the 'union' of two dictionaries.
If the dictionaries each contain the same key, output an error message.
Solution Preview
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
#include <vector>#include <string>
#include <initializer_list>
using namespace std;
/*
* A pair could be a vector, or a struct, or a class.
* You can follow the pattern that a key is a string, and a value is an int.
*/
struct Pair {
string key;
int val;
Pair(){
};
Pair(string key_, int val_) {
key = key_;
val = val_;
}
};
class Dictionary {
public:
Dictionary();
Dictionary(const Dictionary& orig);
virtual ~Dictionary();
vector<string> keys();
vector<int> values();
vector<Pair *> items();
void add(Pair p);
void del(string key);
bool in(string key);
int get(string key);
/**
* union operator
* @param d
* @return
*/
Dictionary operator+(const Dictionary& d);
/**
* we have to use initializer_list to use
* python assignment style
* grades = {'jo':55, 'moe':97, 'will':74, 'bill':80)}
* @param p
*/
void operator=(initializer_list<Pair> p);
/**
* normal assignment operator overloading
*
* @param d
* @return
*/
Dictionary operator=(const Dictionary& d);
private:
void resize();
bool hasKeyOf(const Dictionary & d);
private:
//The implementation should use a 'vector' to store the pairs.
/*
* NOTE: if there is any kind of similar class in some version of C++,
* you should ignore it. :) The idea is to bake this cake from scratch.
*/
Pair * dict; // array of pairt
int size; // number of pair
int capacity; // capacity of the array
};
#include <iostream>
#include <initializer_list>
using namespace std;
Dictionary::Dictionary() {
size = 0;
capacity = 10;
dict = new Pair[capacity];
}
Dictionary::Dictionary(const Dictionary& orig) {
dict = new Pair[orig.capacity];
size = orig.size;
capacity...