0

I have a nested map with all self-defined objects as key and values

typedef map <Time,WindLogType> minuteIntervalData;
typedef map <Date,minuteIntervalData> DayData;

How can I access the the data in the inner map by using the operator [] when the inner key is unknown?

Eg.

DayData[date1] //gets time1,time2,time3..... and WindLogType
Boom
  • 95
  • 6
  • 1
    The key part in your question is "when the inner key is unknown". If you want to access a single element of the nested map (for example using the `[]` operator), you *must* know its key. You can still use the map, like for example iterate over it, but it's not possible to get a single element of it. – Some programmer dude Nov 18 '20 at 03:54
  • I'm not sure I understand. You have a key (a `Date`) to the outer map? From that you get a map of type `minuteIntervalData`. From that you want to get all entries in that map? (If so, why mention the outer map at all? Just start with a single `minuteIntervalData` object.) – JaMiT Nov 18 '20 at 03:55
  • @JaMiT In my (```Date```) i have several dates 1/01/2020,2/01/2020 from the dates i want to access the inner map which contains time stamps of the day eg. 9:30,9:40. From there i want to access a Date and get all the timestamps according to the Date key – Boom Nov 18 '20 at 04:02
  • @Boom Think abstractly, and simplify. You appear to know that, given an object `days` of type `DayData`, the expression `days [date1]` evaluates to an object of type `minuteIntervalData`. Done deal. For the next step, abstract away that expression and start with an object of type `minuteIntervalData`. Once you have such an object, it does not matter how you obtained it. You can remove all mention of `DayData` from your question, if I understand what you are trying to ask. – JaMiT Nov 19 '20 at 01:44

1 Answers1

0

The only way to access a map (whether it is an "inner" map or otherwise) with operator[] is by passing the key as the operand. Thus if the key is unknown, you cannot access the values with operator[].

There are other ways to access values of a map though and not all of them require the knowledge of the keys. One way to access the values is to use iterators.

eerorika
  • 232,697
  • 12
  • 197
  • 326