Hi,
I need to maintain a list of vector(int), vector(char) and vector(float). Is this possible?
Hi,
I need to maintain a list of vector(int), vector(char) and vector(float). Is this possible?
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.
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;
}
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!
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:
dynamic_cast may be slow, I wouldn't use it in a tight loop.