Share your repls and programming experiences

← Back to all posts
Python's dict class in C++
ANDREWVOSS (191)

Dictionary

A recreation of Python's dict class in C++

Usage

The class dict is a template:

Usage is similar to Python:

Methods

Remove an item from from the dict and return its value


Return a vector containing every key-value pair in the dict


Remove all key-value pairs in the dict


Remove the most recently added key-value pair andd return it as a vector


Return a vector containing all keys in the dict


Return a vector containing all values in the dict


Return a reference to a key by its value


Thanks for reading! Also, check out the project on Github!

the github repo is usually updated to the newest stable version within 48 hours

Comments
hotnewtop
Whippingdot (717)

Please forgive my big small brain and answer this question - what is this project supposed to be?

ANDREWVOSS (191)

@Whippingdot It's a header file that implements Python's dict type/class. You can use it in other projects, main.cpp contains a small example of how it might be used.

Coder100 (19275)

wait why is there code in the header file

Coder100 (19275)

in a cpp file!
also don't forget to check if the file was defined multiple times, like this:

@ANDREWVOSS

ANDREWVOSS (191)

@Coder100 Oh ok, I'm new to the whole header/library thing

ANDREWVOSS (191)

@Coder100 When I do that, it just gives an error at compile time. What am I doing wrong? My header file is:

and all my code is moved over to dictionary.cpp

Coder100 (19275)

in the dictionary.cpp, don't forget to include dictionary.h! @ANDREWVOSS

ANDREWVOSS (191)

@Coder100 Same error, but the code works when I #include "dictionary.cpp" pretty sure that's not what I'm supposed to do though

Coder100 (19275)

@ANDREWVOSS oh yeah
it's a class, so you have to do something like this:

then, for your cpp file;

sorry about that lol

ANDREWVOSS (191)

@Coder100 No problem. I'm used to C where headers only need to define functions and constants and there are no classes lol

ANDREWVOSS (191)

@Coder100 One last thing, what about the vector variables?

Coder100 (19275)

@ANDREWVOSS oh
don't need to do anything about variables, just leave them as is

Coder100 (19275)

np!
now don't forget to bug other people that don't do this

because it is important :D @ANDREWVOSS

ANDREWVOSS (191)

@Coder100 So turns out templates have to be defined in headers :/ (otherwise you just have to include the .cpp, as well as creating even more templates to handle the original template). Really stinks, since it makes the source code all funky with there not being a .cpp and all that, but it won't work with out the template. Well, at least you reminded me to use include guards. (thanks for all the help, I'll remember all this next time I make a non-template header)

DynamicSquid (5068)

@ANDREWVOSS Don't listen to Coder, he's wrong and it's not as simple as that. Right now, since your code is only 2 files, and it's a template class, putting your code in the header file is perfectly fine. You can read more here on templates in header files for more complicated projects.

ANDREWVOSS (191)

@DynamicSquid Yeah figured out that much last night after some intense googling
That exact stackoverflow question is one of the places I looked for info

DynamicSquid (5068)

Nice! Is there a way to sort it though?

ANDREWVOSS (191)

@DynamicSquid I'm not sure what you mean by "sort through". If you mean getting an item by its index, I plan on adding that feature soon (probably tomorrow bc I have nothing better to do)

DynamicSquid (5068)

@ANDREWVOSS Oh, I just meant if there is a way to sort the dict. That way you can allow a user to iterate through it.

ANDREWVOSS (191)

@DynamicSquid The dict is sorted in the order that the items were added. You can already iterate through list_keys(), list_values(), and items(). If I implement the indexing feature, that will add a way to iterate through the dict without exporting it as another datatype (So you could modify values as you iterate).

ANDREWVOSS (191)

@ANDREWVOSS it would look something like this:

ANDREWVOSS (191)

@DynamicSquid I implemented the index feature, it works exactly like I outlined above.

ANDREWVOSS (191)

@DynamicSquid Actually I had to change the implementation some. Turns out std::pair doesn't play well with references, so there is going to be two index functions, one to get a value, and another for a key. (So the feature won't be as simple as I had hoped, but it should work)