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 ^^
#include <map>
// A representation of a date
struct Date
{ int yyyy;
int mm;
int dd;
// Compare two dates. Needed to order the map.
booloperator < (const Date & dt) const
{ if (yyyy < dt.yyyy) returntrue;
if (yyyy == dt.yyyy && mm < dt.mm) returntrue;
if (yyyy == dt.yyyy && mm == dt.mm && dd < dt.dd) returntrue;
returnfalse;
}
};
// 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
#include <map>
#include <list>
usingnamespace std;
// A representation of a date
struct date
{ int yyyy;
int mm;
int dd;
// Compare two dates. Needed to order the map.
booloperator < (const date & dt) const
{ if (yyyy < dt.yyyy) returntrue;
if (yyyy == dt.yyyy && mm < dt.mm) returntrue;
if (yyyy == dt.yyyy && mm == dt.mm && dd < dt.dd) returntrue;
returnfalse;
}
};
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())
returnfalse; // 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++;
returntrue; // entry updated
}