vector help

Started by
6 comments, last by vaneger 21 years, 8 months ago
is it possible to have a vector that simply stores data regardless of its type? liek for instance vector[0] is an int vector[1] is a char and vector[2] is a *long ?
Advertisement
If you really want to make a strong system, design a class that can allocate a block of memory and keep list of what types are at what memory positions. For example, the list of types could look something like this:
Type  |  Position-----------------int      0float    4char     8long*    9int      13char     17char     18int*     19  


Something like that. Or you could simply create a list of void pointers and allocate the memory for each type yourself, and cast everything, but you still would need some sort of reference for what datatypes are stored. Actually, it depends how you are using it. If you need the datatypes, then you need to store that information, otherwise you can cast everything.

EDIT: Might I ask what you need to do this for?


[edited by - Zipster on July 29, 2002 9:14:50 PM]
Looks like
VARIANT
to me.
"after many years of singularity, i'm still searching on the event horizon"
If you are storing different data types for a vectors components then maybe it's not a vector anymore. Perhaps you should be using some sort of other data structure.... Anyway, of you really want to:

If you want the all the vectors components to be of the same type then you can probably use templates:

        template <typename T>struct Vector{    T x,y,z;};//you can then instantiate the template with:      Vector<int> v; //this will create a vector will int components//also remember to initialize all the components (x,y,z)    


or if you want each component to be a different data type:

  template <typename A,typename B,typename C>struct Vector{    A x;    B y;    C z;};   //you can then instantiate the template with:      long test = 3415;Vector <int,char,long*> weirdV;weirdV.x = 2;weirdV.y = 'c';weirdV.z = &test  




[edited by - bslayerw on July 29, 2002 12:23:30 AM]
well using templates is one way but thats annoying to have to go vector etc and i have a templated vector class any way. what i was thihking of trying is sounding more like zipsters idea of void pointers though ive never doen such a thing so coudl u post some code on that? i have a method of getting the data type using tyeinfo.h so thats no problem. the reason i wanted to do this is so i could create a singel vector with a few similar data types in it and then parse them as needed using typeinfo to get its data type then calling the apporiate functions
What are you using the vector for? void pointers is in most cases a bad idea and parsing out the type and cast thing is expensive.
from my other web browser window:

http://www.boost.org/libs/any/index.html


Don''t listen to me. I''ve had too much coffee.
Look-up heterogeneous containers. I saw a working implementation last time I Googled the subject.

Cédric

This topic is closed to new replies.

Advertisement