List of different types of vectors?

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

+3
source share
4 answers

I'm not sure I understand your question.

Is boost::variantwhat you are looking for? This will allow you to store items with different types in one container.


Code example (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;
}
+5
source

You can 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 an int vector.\n";
            }
            else if (dynamic_cast<VectorWrapper<char>*>(v) != 0) {
                std::cout << "It a char vector.\n";
            }
            else if (dynamic_cast<VectorWrapper<double>*>(v) != 0) {
                std::cout << "It a double vector.\n";
            }
        }

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

This has some disadvantages:

  • You need to use pointers, so be careful with memory leaks.
  • dynamic_cast maybe slow, I would not use it in a narrow loop.
+2
source

, " ", , , .

If so, is simple enough union? e.g. (Unverified):

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 shit, that won't work. You cannot put vectorin union, because they have copy constructors. Excuse me!

+1
source

Ok, now I understand what is better. You must create 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.

0
source

Source: https://habr.com/ru/post/1763325/


All Articles