Python's dict class in C++


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


The class dict is a template:

template<key_type, val_type>
class dict

Usage is similar to Python:

dict<string,string> d;
d["foo"] = "bar";


val_type pop(key_type key)

Remove an item from from the dict and return its value

std::vector<std::pair<key_type, val_type>> items()

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

void clear()

Remove all key-value pairs in the dict

std::pair<key_type, val_type> pop_item()

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

std::vector<key_type> list_keys()

Return a vector containing all keys in the dict

std::vector<val_type> list_values()

Return a vector containing all values in the dict

key_type& get_key_by_value(val_type val)

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

You are viewing a single comment. View All
Coder100 (18806)

wait why is there code in the header file

Coder100 (18806)

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

#ifndef PY_DICT
#define PY_DICT

// definitions




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


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

#ifndef PY_DICT
#define PY_DICT

template<typename key_type, typename val_type>
class dict;


and all my code is moved over to dictionary.cpp

Coder100 (18806)

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


@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 (18806)

it's a class, so you have to do something like this:

// header:
template<typename key_type, typename val_type>
class dict {
  val_type pop(key_type key);
  val_type& operator[](key_type key);
  // ...

then, for your cpp file;

val_type dict::pop(key_type key) {

sorry about that lol


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


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

Coder100 (18806)

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

// header
class dict {
  int a;
Coder100 (18806)

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

because it is important :D @ANDREWVOSS


@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 (5016)

@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.


@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