one vector for two class

how to i create a vector that holds two class' type?
Use a vector of std::pair
May be by creating a class that contains the two class types you want and then creating the vector of that new class?
i thınk you mısunderstand me firedraco. I want a vector that;

let's say it's name is "vect". and vect.at(i) can hold integer vect.at(i+1) can hold string.
(any element of vector can hold integer -or- string)

and kevinchkin, how can ı do that. it does not much dıffer from holding two separate vector. right?

thank you for answers.
Vectors, being arrays, are unable to hold differently-sized elements.
There are two possibilities in this case:
1. Use two vectors, one for each type: std::vector<int>/std::vector<std::string>
2. Use a vector that holds structures, each of which holds both types. std::vector<std::pair<int,std::string> > (note the space between the greater-than symbols. It's mandatory)
I don't know details of your program, but you may try to use a vector of pointers to some base class. Object stored in the vector should be of types derived from this base class. Additionally you may use some kind of smart pointer to make memory management easier (std::auto_ptr cannot be used since it does not have a proper = operator).

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
template <typename T_base>
class smart_pointer_t
{
public:

   template <typename T_derived>
   smart_pointer_t(T_derived * ptr_) : ptr(ptr_){}

   ~smart_pointer_t(){ delete ptr; }

   ...

private:

   T_base * ptr;
};

class base_t
{
   ...
};

class derived_1_t : public base_t
{
   ...
};

class derived_2_t : public base_t
{
   ...
};

int main()
{
   std::vector<smart_pointer_t<base_t> > table;

   table.push_back(smart_pointer_t(new derived_1_t));
   table.push_back(smart_pointer_t(new derived_2_t));

   return 0;
}
Other options are

 
std::vector< boost::variant< type-1, type-2, ... > >


If you know the types at compile time and there are only a couple of them (by default variant supports up to 9
different types, IIRC).

Or

 
std::vector< boost::any >


If you only know the types at runtime, but then it becomes a bit of a problem extracting values, since in order
to get the value out of a boost::any you must any_cast<> it to the correct type. (If you get the type wrong,
any_cast will throw if casting to reference or return NULL if casting to pointer).

how can ı do that. it does not much dıffer from holding two separate vector. right?


I guess pair is better way to go. I didn't know about it. But anyways what I was saying that you can do something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

class  A { 

   public:
      int a; 
} ; 

class B { 
    public:
         int b ; 
} ; 

class C { 
     public: 
        A Obj1 ; 
        B Obj2 ; 
} ;  

int main( ){ 
  vector<C> myVec; 
  return 0 ; 
}
Or as kevinchkin said before, just make a vector of a class that contains an integer and a string.

1
2
3
4
class A {
int a;
char* b;
};


You can then make an array of said class, where vect[i].a is an integer and vect[i].b is a string.
kevinchkin: What would you gain from doing something like that? I mean, what's the purpose of A and B? Why not just put their members in C?
@helios: I will not gain anything. :-)
The OP mentioned that he wanted vector to hold two classes that's why I just showed an example.
Topic archived. No new replies allowed.