I'm practicing various C++ exercises. In this particular exercise enter image description herethey are asking me to implement an in memory database. I guess they are not asking me to use something like SQLlite. Could anybody give some general pointers as to how I should tackle this please? Would I use C++ maps. I'm struggling to understand what they mean by in memory?

| improve this question | | | | |
  • 2
    "In memory" usually means "in RAM" as opposed to "on hard drive". – Mael Feb 28 '18 at 14:05
  • SQLite can run in memory -- sqlite.org/inmemorydb.html Essentially, there is no disk access. Creating your own database means you have full control over the structures you use, but does not necessarily mean you have to implement SQL as long as you have an API. – Berin Loritsch Feb 28 '18 at 14:12
  • 3
    Don't take the word "database" in this exercise too literally. A vector with triples of the form (type, category, item) for storing the data should be enough to solve this task. – Doc Brown Feb 28 '18 at 14:56
  • @DocBrown Can the one vector store multiple types of item? – ChrisW Feb 28 '18 at 14:57
  • 1
    Recommended reading: Open letter to students with homework problems. "If your question... is just a copy paste of homework problem, expect it to be downvoted, closed, and deleted - potentially in quite short order." – gnat Feb 28 '18 at 20:22

I assume you're allowed (expected) to use the STL (or whatever it's called these days -- the "standard library", probably).

To "store items of distinct types", use some template container(s).

To support "item belongs to one category", store item+category pairs (which is more-or-less how a database would do it), or sets of items for each category (possibly a single map with category as key and set of items as values).

It says "distinct" types so perhaps you can't assume the types are related at all.

"In memory" means it doesn't need to be serialized or saved to disk ... and presumably mustn't use an existing database implementation which isn't part of the C++ standard library.

It's not clear to me what the "identity" of an item is. E.g. does every type have an ID? Or do you pass the address of (i.e. a pointer to) an item as a parameter to the remove method?

| improve this answer | | | | |
  • Hi @ChrisW yes I thought the same about using the STL. Do you think I should use a std::map? Thanks – Tom Dara Feb 28 '18 at 14:35
  • 2
    Maybe. When I think of "databases" (i.e. tables) I think of them as a sorted list of records. A table's index is analogous to a map (from key to record) for the table. But the specs you quoted don't mention keys or indexes (see the comment about "identity" at the end of my answer). Whatever, I'd suggest a simple+correct implementation is preferable to a complex+incorrect one. You might want to write the test cases first. After it's coded and tested you could post it on Code Review for further commentary. – ChrisW Feb 28 '18 at 14:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.