2d vector of different data types

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:

1
2
3
4
5
6
7
8
9
  vector <vector<int> > numbers;

  for(int i=0; i<5; i++){
      vector <int> row;
      for(int j=0; j<5; j++){
          row.push_back(i+j);
      }
      numbers.push_back(row);
  }

And access value as in multidim.array.
But can I create smth like that:

1
2
3
4
  class A{};
  class B{};

  vector <A <vector <B> > > objects_of_classes;


Like class A has a member-vector of objects of class B. And I need a vector of objects of class A.

Thanks for any information :) And sorry for possible mistakes.
Last edited on
No, go back a stage, @olyaMi. What are you actually trying to do? Then, and only then, can we think about some peculiar fiddle with vectors.
Like class A has a member-vector of objects of class B. And I need a vector of objects of class A.
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
#include <iostream>
#include <string>
#include <vector>

class B {
  public:
    int doge = 3;
};

class A {
  public: 
    std::vector<B> vb;
    
    A()
    : vb(5)
    { }
};


int main()
{
    std::vector<A> ab;
    
    ab.push_back( A() );
    
    ab[0].vb[1].doge = 2;
}
Ha, that's brilliant!

Over to you now, @olyaMi. What are you going to do with these classes now you have them?
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.
Last edited on
@lastchance, I was trying to create kind of a datebook:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Event {
     friend class Day;
     //..vars about event (name, place, etc)..
     // ..getter and setter funcions, constructor..
};

class Day {
     friend class 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 :)

PS: @Ganado and @jonnin, thanks for the answers!
Last edited on
@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!
Topic archived. No new replies allowed.