0

During my C++ exam I found I had some difficulties with what is probably supposed to be something relatively simple. I think I am overlooking something on my problem. For the sake of not getting in trouble for posting part of an exam online, I will make a very simple representation of the part I had troubles with.

What was expected was to make a class myClass in which i could add items item. Then i was supposed to be able to make lists of some items i wanted in that list. The amount of items able to be in the list should be infinite. Each list should have a certain (unique) name. I am also told not to make a new class for the lists and to choose the right container myself.

myClass::myClass(){
    itemlist= {};
    lists= {};
}

void myClass::addItem(Item &item){
    itemlist.emplace_back(item);
}

void myClass::makeList(string listname){
    vector <Item> list = {};
    // list .name = listname
    lists.emplace_back(list);
}

void myClass::addItemToList(string listName, Item &item){
    for (int i=0; i<lists.size; i++){
        if lists[i].name == listName {
            lists.emplace_back(item);
            return;
        }
    }
}

I don't know how to link a name to a list without creating a new class for the lists. I thought about storing another list containing the names of the lists and using that, but it seemed wrong to do so, as they aren't actually linked then. Tried to look up if there is a way to make a list variable listname from the string listname parameter, but apparently that's not possible.

As for the containers, I chose a vector as they can store an "infinite" amount of data in it. I use the itemlist to store all items ever added to myClass, and lists to store all the lists ever created.

In what way is it possible to store a name for each list and for it to be linked?

hnefatl
  • 5,860
  • 2
  • 27
  • 49
Christoph
  • 114
  • 1
  • 1
  • 10
  • 1
    I think `list` is a bad name for a `std::vector` – Blasco Feb 01 '18 at 08:15
  • It's very nice that you simplified the code, not only because of the exam but to make it easier to help. But please provide a [Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Blasco Feb 01 '18 at 08:16
  • Possible duplicate of [vector or map, which one to use?](https://stackoverflow.com/questions/454762/vector-or-map-which-one-to-use) – Lanting Feb 01 '18 at 08:17
  • Well these are not my actual variable names, I tried to make it as clear as possible with simple names. – Christoph Feb 01 '18 at 08:17
  • As you're keeping a collection of lists inside your class, just make a `std::map>`, keyed on the list's name and with values of the lists themselves. – hnefatl Feb 01 '18 at 08:19

2 Answers2

2

I think the simplest way to name lists is to use std::unordered_map http://en.cppreference.com/w/cpp/container/unordered_map

Then you'll be able to use your lists like this:

lists["first_list"].push_back(...);
lists["another_list"].size();
lists["create_new_list"] = {};
Eugene Kosov
  • 993
  • 7
  • 15
1

Maybe what you are looking for "link a name to a list" is a std::map

Something like std::map<std::string, std::vector<Item>>

Germán
  • 581
  • 1
  • 4
  • 19