tags:

views:

122

answers:

4

Hi,

I need to maintain a list of vector(int), vector(char) and vector(float). Is this possible?

A: 

Ok, I think now I understand it better. You should make a wrapper class for each type of vector that you may encounter, and make all of them inherit from the same base class, say AbstractVector. Then you can have a list of AbstractVector objects.

Dima
the problem is that I dont know beforehand what the vector types are gonna be, I get that info dynamically and create the vector types
Rajesh
could you elaborate a bit?
Rajesh
DerKuchen's answer is exactly what I was talking about. However, just keep in mind, that C++ isn't very well suited for dealing with collections of mixed types. You seem to be fighting the language, and that may be an indication of a deeper problem with your design, or it simply may be that C++ is not the best language for your project.
Dima
+3  A: 

I am not sure I understand your question.

Is boost::variant what you are looking for? It will allow you to store the elements with different types in a single container.


Some sample code (not tested):

typedef boost::variant <
  std::vector<int>, 
  std::vector<char>, 
  std::vector<float>
> VectorOfIntCharOrFloat;
std::list<VectorOfIntCharOrFloat> vec;

and then iterate over it / access elements as:

std::list<VectorOfIntCharOrFloat>::iterator itr = vec.begin();
while(itr != vec.end()) {
  if(std::vector<int> * i = boost::get<std::vector<int> >(itr)) {
    std::cout << "int vector"<< std::endl;
  } else if(std::vector<float> * f = boost::get<std::vector<float> >(itr)) {
    std::cout << "float vector" << std::endl;
  } else if(std::vector<char> * c = boost::get<std::vector<char> >(itr)){
    std::cout << "char vector" << std::endl;
  }
  ++itr;
}
missingfaktor
Prefer use of ++itr - it's faster.
Steve Townsend
@Steve: Done. Thanks.
missingfaktor
@steve: why is ++itr faster than itr++ in this case (or in general)? Thanks.
Because it doesn't have to create a copy of itr before incrementing.
DerKuchen
A: 

From the conversation thread on the original question, my understanding of the problem is that you have some "black-box" function whose interface is a list of vectors, whose individual types are known at runtime on both sides of the interface, but not represented in the interface itself.

If this is the case, would a simple union suffice? e.g. (untested):

typedef union
{
    std::vector<char>    c;
    std::vector<int>     i;
    std::vector<float>   v;
} unknown_t;

std::list<unknown_t> my_list;

unknown_t u1;  // assume int
unknown_t u2;  // assume float

u1.i.push_back(5);
u1.i.push_back(10);

u2.f.push_back(23.4f);
u2.f.push_back(19.2f);
u2.f.push_back(1e6);

my_list.push_back(u1);
my_list.push_back(u2);

UPDATE

Oh, crap, this won't work. You can't put vectors in a union, as they have copy-constructors. Sorry!

Oli Charlesworth
how would I pass the vector to the blackbox function if it is of type "union", thats not recognized
Rajesh
the blackbox function only recognizes vectors of standard types
Rajesh
@rajesh: It sounds from your question as if you have no control whatsoever over the interface (i.e. it already exists). If that is the case, please add to your original question details on *exactly* what the existing interface is that you have to adhere to.
Oli Charlesworth
As far as I remember, a union of non POD types causes undefined behaviour. §9.5.1 says: "An object of a class with a non-trivial default constructor (12.1), a non-trivial copy constructor(12.8), a non-trivial destructor (12.4), or a non-trivial copy assignment operator (13.5.3, 12.8) cannot be a member of aunion, nor can an array of such objects."
DerKuchen
ok...i think this will work...thanks!
Rajesh
@Oli: Union of non-POD types cause UB. Consider using `boost::variant` instead.
missingfaktor
@DerKuchen: I just realised exactly this, so have updated my answer! Please down-vote it...
Oli Charlesworth
oh..shoot...ok..i'll give it a try..Thanks everyone
Rajesh
+1  A: 

You could try the following:

    #include <iostream>
    #include <vector>

    struct WrapperBase
    {
        // WrapperBase needs to be polymorphic for dynamic_cast
        virtual ~WrapperBase()
        {  }
    };

    template <typename T>
    struct VectorWrapper : public WrapperBase
    {
        std::vector<T> vector;
    };

    int main()
    {
        std::vector<WrapperBase*> vectors;

        vectors.push_back(new VectorWrapper<int>());
        vectors.push_back(new VectorWrapper<char>());
        vectors.push_back(new VectorWrapper<double>());

        for (int i=0; i < vectors.size(); ++i) {
            WrapperBase *v = vectors[i];
            if (dynamic_cast<VectorWrapper<int>*>(v) != 0) {
                std::cout << "It's an int vector.\n";
            }
            else if (dynamic_cast<VectorWrapper<char>*>(v) != 0) {
                std::cout << "It's a char vector.\n";
            }
            else if (dynamic_cast<VectorWrapper<double>*>(v) != 0) {
                std::cout << "It's a double vector.\n";
            }
        }

        for (int i=0; i < vectors.size(); ++i) {
            delete vectors[i];
        }
    }

This has some drawbacks though:

  • You need to use pointers, so be careful with memory leaks.
  • dynamic_cast may be slow, I wouldn't use it in a tight loop.
DerKuchen
`boost::variant` provides this exact functionality, so you're basically reinventing the wheel. Anyway as OP said R can recognize only lists of vector or primitive data types, so this solution also won't work.
missingfaktor