1

This is somewhat similar to this problem 4d mapping in C++? and one of my previous questions on maps in C++ Use a map with the map name defined by a string C++

I have a code that looks like this (that does not work and stops at the line giving input to the map):

#include <iostream>
#include <string>
#include <tuple>
#include <map>
#include <vector>

using namespace std;

int main()
{
    map<string, //Volkswagen (car brand)
        map<string, //series
            map<int, //in this example 1
                tuple<string, string>>>> mapMapMap;

    string myCarmapMapMap = "Volkswagen";
    int Number = 1;

    mapMapMap[myCarmapMapMap]["3 series"][Number] = {90, 20};,

    string Output;
    Output.assign(get<0>(mapMapMap[myCarmapMapMap].find("3 series")->second));
    cout << "\n" << "------------" << "\n" << Output << "\n"<< "------------" << "\n";
}

What I want to do is to assign two values to Volkswagen, 3 series, 1 and then be able to call it like: Volkswagen -> 3 series -> 1 -> <0> (value 1).

This is the error message I get: |19|error: expected primary-expression before ',' token|

I have also tried:

mapMapMap.insert({myCarmapMapMap, "3 series", Number, {"90", "20"}});

But it does not work either. How do I combine a 4d map with a tuple?

KGB91
  • 630
  • 2
  • 6
  • 24
  • 2
    You expect a `tuple` but you are trying to assign integers `90` and `20`. – Timo Oct 14 '19 at 14:26
  • The error message you get should contain enough information to solve the problem. – Some programmer dude Oct 14 '19 at 14:30
  • True, but putting "" around did not help. – KGB91 Oct 14 '19 at 14:30
  • Then you need to tell us what you mean by "it does not work". Do you get build errors? Please copy-paste the full and complete error output into the question. And please refresh [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Oct 14 '19 at 14:31
  • Thanks, I updated with the error message. – KGB91 Oct 14 '19 at 14:32
  • `mapMapMap[myCarmapMapMap].find("3 series")->second` is a `map>`. You're one level short on the lookups. – molbdnilo Oct 14 '19 at 14:35
  • Like this then? `Output.assign(get<0>(((mapMapMap[myCarmapMapMap].find("3 series"))[Number])->second));` – KGB91 Oct 14 '19 at 14:37
  • @KGB91 No. `mapMapMap[myCarmapMapMap].find("3 series")` is a map iterator; you can't index it. Stop guessing, think about the types. – molbdnilo Oct 14 '19 at 14:39

1 Answers1

2

Change your assignment so it can actually form a tuple<string, string> (note the quotation signs on the right hand side):

mapMapMap[myCarmapMapMap]["3 series"][Number] = {"90", "20"};

Example

Also, remove the , at the end of the line.

Your query can, probably, be fixed by including the Number again, like:

string output = get<0>(mapMapMap[myCarmapMapMap].find("3 series")->second[Number]);
Timo
  • 9,269
  • 2
  • 28
  • 58
  • That helped, but I still cannot search for the content I want. `|22|error: no matching function for call to 'get(std::map, std::allocator >, std::__cxx11::basic_string, std::allocator > > >&)'|` – KGB91 Oct 14 '19 at 14:33
  • Yes, because that query gives you a `map>` and not a `tuple`. That's why `get` is not working. – Timo Oct 14 '19 at 14:36
  • How do I write the last step? See comment above for my try... – KGB91 Oct 14 '19 at 14:38