Uso de arrays como valor de un map

Buenas, la cosa es que quiero hacer un map donde el valor sea un array de structs y no se como narices modificar el valor del array.
La cosa es que hago un map donde la clave es una fecha y el array es de 24 posiciones(uno por hora del dia) y dados una fecha, hora y datos quiero ir aƱadiendo estos datos en su lugar pero no se modificar el valor del map ni nada. si me pudieran guiar por donde tirar se lo agradeceria mucho ^^

Hi, the fact is that i want to do a map where the value is an array of structs(something like: map<date,dictionary[24]> dmap; dictionary is an struct i have declared before).The thing is that i want a map where the key is a date and the value is the array os structs with 24 positions(24 hours) and after a date, an hour and some other informations is entered i want to add this information to the correct place on the map and tha struct, but i dont know how to modify the value of the map, i so lost on the set and the maps if anyone could give me some orientation of what to do to enter the info on the array of the map ^^
Last edited on
Something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <map>
//  A representation of a date
struct Date
{   int yyyy;
    int mm;
    int dd;
    //  Compare two dates.  Needed to order the map.
    bool operator < (const Date & dt) const
    {   if (yyyy < dt.yyyy) return true;
        if (yyyy == dt.yyyy && mm < dt.mm) return true;
        if (yyyy == dt.yyyy && mm == dt.mm && dd < dt.dd) return true;
        return false;
    }                 
};

//  Some data organized by hour    
struct hourly_data
{   int num[24];
};

int main ()
{   std::map<Date, hourly_data>  my_map;
    Date        date;
    hourly_data hours;

    //  Set the date    
    date.yyyy = 2017;
    date.mm = 11;
    date.dd = 7;
    //  Initialize the hourly data
    for (int i=0; i<24; i++)
      hours.num[i] = 0;
    //  Crate a pair      
    std::pair<Date, hourly_data>    pr (date, hours);
    //  Insert the pair into the map
    my_map.insert (pr);
}

Well I use it to register the amount of cars that pass through a road and information related to these cars like the speed, the colour, type (car, motorbike,...), for the moment i only need to store cars info and what i do is something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct diccars
    {
        date d;
        short hour;
        short min;
        short sec;
        int speed;
        string colour;
    };

    struct dic
    {
        list<diccars> llista;
        unsigned counter;
    };

    map<date, dic[24]> dmap;
[/code]
so my main problem is when i get the info and have to modify the array and list of some day. What function/process would u do? Thanks
Last edited on
I've reorganized your structs slightly. You can't put an array dimension on a type passed to a template.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <map>
#include <list>
using namespace std;

//  A representation of a date
struct date
{   int yyyy;
    int mm;
    int dd;
    //  Compare two dates.  Needed to order the map.
    bool operator < (const date & dt) const
    {   if (yyyy < dt.yyyy) return true;
        if (yyyy == dt.yyyy && mm < dt.mm) return true;
        if (yyyy == dt.yyyy && mm == dt.mm && dd < dt.dd) return true;
        return false;
    }                 
};

struct diccars
    {   date d;
        short hour;
        short min;
        short sec;
        int speed;
        string colour;
    };

//  What we want to track by the hour
struct hourly_data
{   list<diccars> llista;
    unsigned counter;
};
    
//  Data for a day, organized by hour     
struct dic24
    {   hourly_data data[24];
    };

int main ()
{   map<date, dic24>  dmap;
    date        d;
    dic24       dic;

    //  Set the date    
    d.yyyy = 2017;
    d.mm = 11;
    d.dd = 7;
    //  Initialize the hourly data
    for (int i=0; i<24; i++)
      dic.data[i].counter = 0;
    //  Crate a pair      
    pair<date, dic24>    pr (d, dic);
    //  Insert the pair into the map
    dmap.insert (pr);
}

//  Update a map entry for a specified hour
bool update_entry (std::map<date, dic24> & dmap, date & d, int hh)
{   map<date, dic24>::iterator   iter;
    hourly_data *                data;
    diccars                      car;
              
    iter = dmap.find (d);    // find the entry in the map for the date
    if (iter == dmap.end())
        return false;   //  entry for date not found.  Probably want to add one.
    //  *iter now points to a dic24 instance
    //  Create a pointer to the hour's data we want to modify
    data = &iter->second.data[hh];
    //  Add a car to the list
    car.d = d; 
    car.colour = "red";
    car.speed = 30;
    car.hour = hh;
    data->llista.push_back (car);
    //  Update the counter for the hour
    data->counter++;
    return true;        //  entry updated
}


Registered users can post here. Sign in or register to post.