Hello! I'm new to c++ and have encountered a problem.
Is it possible to create a multidimensional vector of different datatypes?
I've found only how to create such vector of 1 datatype, like this:
the above is a 2-d of class B, and a neat trick, as it also has a 1-d of class A at your disposal, but it is NOT a 2d of both A and B as a mashup. To do that...
or are you asking for a 2-d array such that thing[r][c].A and thing[r][c].B exist?
you just need to wrap A and B into one thing, then make a 2-d array of them (simplest possible example here):
struct c {A a; B b;} //where A and B are whatever types.
vector<vector<c> >;
c[r][c].a.objectstuff; //ok
c[r][c].b.foo(); //ok
or you can enjoy the virtues of C and C++ both.
vector <vector < void*> > vp; //ouch. There be dragons. Then you could do something 'exciting' like have every even row of type A and odd rows of type B.
class Event {
friendclass Day;
//..vars about event (name, place, etc)..
// ..getter and setter funcions, constructor..
};
class Day {
friendclass Event;
//...some vars for date..
public:
vector <Event> events;
void add_event(Event e){
events.push_back(e);
}
// ..getter and setter funcions, constructor..
~Day(){
events.clear();
}
};
And I saved it to file.
The problem arose when I decided to add deletion.. For that purpose I first thought about a vector (not array cause I didn't want to traverse my file several times, and I didn't store info about number of days and events anywhere) of Days, and each day has a vector of its Events.
I know that it would be better to have just one class for event with date, but I was just wondering how to do it if it's possible :)
@olyaMi,
I think you have a database structure. I don't think Events are internal to Days, nor Days internal to Events, as changing either of those properties would be a nightmare.
Others may have better ideas, but I would keep Day and Event classes completely separate and another container that tied up Day and Event pairs.
@lastchance, thank you for the idea! I might go for it.
As to my initial question: @Ganado's solution seems to be the closest thing to what I wanted initially. Solved!