Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list of different vector types? [closed]

Tags:

c++

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

like image 390
Rajesh Avatar asked Dec 29 '22 07:12

Rajesh


1 Answers

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;
}
like image 81
missingfaktor Avatar answered Jan 08 '23 17:01

missingfaktor