Question

I need help defining a class in C++ that acts like a python dictionary. I need...

I need help defining a class in C++ that acts like a python dictionary. I need to be able to add keys-values to an already existing dictionary. For example

id= {'sam':75, 'robert':09907, 'timmmy',95453, 'samuel',5333)}.

i want to create a class in which i can do

id.add("samuel', 8439922) and it adds it to the dictionary. i want to implement this with using vectors to store the pairs.

I need to do this without using any features like maps, every thing has to be made from scratch.

edit: does not need to be generic, can be with strings and ints

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi Friend,

To implement a requirement of key value combination, it is advised create a class with the values you require and implement any data structure you want.

In this case, you require a combination of a string and int to be accessed in pairs.

To do that,

1. Create a class with two variables with access specifier as public (to reduce the effort to store and retreive).

2. You can define vectors for user defined data types as vector<Classname> variable.

3. Create an object for the class and assign the values.

4. push the object into the vector using inbuilt function push_back(object).

5. You can find the size of the vector using size() fucntion. With that you can access the values using iterator.

ACTUAL CODE:

/* C++ program to implement python Dictionary */

// header files
#include <iostream>
#include <string>
#include <vector>

using namespace std;

/*
* Define a class Dictionary with member variables
String name and Integer id as public so that they
can be accessed directly with objects.
*/

class Dictionary
{
public:
   string name;
   int id;
} d;


// Driver program
int main()
{
   // Create a vector called dlist which holds all the key value pairs of the dictionary
   vector<Dictionary> dlist;
  
   // Assign value to name and id using object d for the class Dictionary
   d.name = "sam";
   d.id = 75;
  
   // To add the current entry to the dictionary, use ibuilt function push_back() which
   // appends entry from behind
   dlist.push_back(d);
  
   // Continue for other values as well
   d.name = "robert";
   d.id = 9907;
   dlist.push_back(d);
   d.name = "timmmy";
   d.id = 95453;
   dlist.push_back(d);
   d.name = "samuel";
   d.id = 5333;
   dlist.push_back(d);
  
   // To display the entries of the dictionary
   // use iterator i

   cout << "Name\tID" << endl;
   cout<< "---------------" << endl;
   for (int i = 0; i < dlist.size(); i++)
   {
       cout << dlist[i].name << "\t" << dlist[i].id << "\t" << endl;
   }
   return 0;
}


SAMPLE OUTPUT:

I hope this solution helps you. Please give an upvote.

Thank you.

Add a comment
Know the answer?
Add Answer to:
I need help defining a class in C++ that acts like a python dictionary. I need...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help defining a class in C++ that acts like a python dictionary. I need...

    I need help defining a class in C++ that acts like a python dictionary. I need to be able to add keys-values to an already existing dictionary. For example id= {'sam':75, 'robert':09907, 'timmmy',95453, 'samuel',5333)}. i want to create a class in which i can do id.add("samuel', 8439922) and it adds it to the dictionary. i want to implement this with using vectors to store the pairs.

  • I need help with this python programming exercise, please! thanks in advance Create a Python script...

    I need help with this python programming exercise, please! thanks in advance Create a Python script file called hw4.py. Add your name at the top as a comment, along with the class name and date. Both exercises should be in this file, with a comment before each of them to mark it. Ex. 1. Write a program that inputs an integer number from the user, then prints a letter "O" in ASCII art using a width of 5 and the...

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • Hi, I have a python coding questions and would need help: Count the neighbors of each node in a g...

    Hi, I have a python coding questions and would need help: Count the neighbors of each node in a graph. input graph is a multi-dimensional list Let's say I want to have each person's friends count: input (list of list): [[A, B], [B, C], [B, D], [E]], output (dict): {A:1, B: 3, C:1, D:1, E:0} assume you won't get a repeat pair like [A,B] and [B,A], and neither will there be more than 2 people in a relationship. I want...

  • I need some help i need to do this in C# Objectives: • Create an application...

    I need some help i need to do this in C# Objectives: • Create an application that uses a dictionary collection to store information about an object. • Understanding of abstract classes and how to use them • Utilize override with an abstract class • Understanding of Interfaces and how to use them • Implement an Interface to create a “contract” between classes. • Compare and contrast inheritance and interfaces. Instructions: Interface: Create an interface called ITrainable which contains the...

  • I need help with this problem. Using Python please. Thanks Construct a class “Monster” with the...

    I need help with this problem. Using Python please. Thanks Construct a class “Monster” with the following attributes: self.name (a string) self.type (a string, default is ‘Normal’) self.current_hp (int, starts out equal to max_hp) self.max_hp (int, is given as input when the class instance is created, default is 20) self.exp (int, starts at 0, is increased by fighting) self.attacks (a dict of all known attacks) self.possible_attacks (a dictionary of all possible attacks The dictionary of possible_attacks will map the name...

  • I am having trouble with my Python code in this dictionary and pickle program. Here is...

    I am having trouble with my Python code in this dictionary and pickle program. Here is my code but it is not running as i am receiving synthax error on the second line. class Student: def__init__(self,id,name,midterm,final): self.id=id self.name=name self.midterm=midterm self.final=final def calculate_grade(self): self.avg=(self.midterm+self.final)/2 if self.avg>=60 and self.avg<=80: self.g='A' elif self.avg>80 and self.avg<=100: self.g='A+' elif self.avg<60 and self.avg>=40: self.g='B' else: self.g='C' def getdata(self): return self.id,self.name.self.midterm,self.final,self.g CIT101 = {} CIT101["123"] = Student("123", "smith, john", 78, 86) CIT101["124"] = Student("124", "tom, alter", 50,...

  • i need help with this python assignment asking for creating a simple database (client application )...

    i need help with this python assignment asking for creating a simple database (client application ) そ归乔 Assignment rh ENCS-393-2174-WW: Tset5.pdf A2.pdf textbook.pdf Department of ComputV ← → O仚 file:///C 18/comp%20348/A2.pdf 5. Print Report: This will print the contents of the database, sorted by name. Note that the sorted contents should be sent by the server back to the client and then displayed by the client app. The Print Report function is r because it is the primary way for...

  • C++ need help programming something like this? This project will help you show your mastery of...

    C++ need help programming something like this? This project will help you show your mastery of arrays, C-strings, classes, and libraries. Write a program to handle a user's rolodex entries. (A rolodex is a system with tagged cards each representing a contact. It would contain a name, address, and phone number. In this day and age, it would probably have an email address as well.) Typical operations people want to do to a rolodex entry are: 1) Add entry 2)...

  • I need help making this in C++ Asap! If you feel information is missing, or provided...

    I need help making this in C++ Asap! If you feel information is missing, or provided information is incorrect, make assumptions but clearly state your assumptions. You should NEVER use global variables. Other than the main() function, there should be no global functions in your code. Other than the test() function not other function needs to be a static function in this problem. Let us say you find a consulting opportunity for a regional real-estate company that wants you to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT