vectors of vectors of different types...
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: vectors of vectors of different types...

  1. #1
    Join Date
    Jul 2010
    Posts
    3

    vectors of vectors of different types...

    Hi,

    So my problem is that I need to create a data table in which each column is a vector that contains some sort of data that is read in from a file. The problem is that the table could have any dimensions, and each column can be one of many types (string, int, double, etc.). I would like to know if there is a way to create this object that I want. Right now I am attempting to do it using a template class:

    Code:
     template <typename T2>
    class asciiColumn
    {
        vector<T2> vec;
        public:
            asciiColumn(vector<T2>);
            vector<T2> read();
    
            asciiColumn (vector<T2> invec)
            {
                vec = invec;
            }
            
            asciiColumn() {}
    
            vector<T2> read()
            {
                return vec;
            }
    };
    The problem with this is that to instantiate a vector of asciiColumns I have to specify the type that asciiColumn should contain, and that defeats the purpose.

    Any help with this solution or other proposed solutions would be much appreciated.

    Thanks, Tommy

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,350

    Re: vectors of vectors of different types...

    What is stopping you from say, storing the data read as strings, and then converting to the desired type when needed?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jul 2010
    Posts
    3

    Re: vectors of vectors of different types...

    Haha nothing i suppose. I'm a little embarrassed that I didn't think of that. That would also make reading in from files a lot easier... thanks!

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,725

    Re: vectors of vectors of different types...

    Another way would be to create a base value class and derive value types from it. The vector would store pointers to base types. This would require you to manage the allocation and destruction of the elements.

    Code:
    struct Value
    {
    };
    
    struct Int_Value : public Value
    {
        int value;
    };
    
    struct Double_Value : public Value
    {
        double value;
    };
    
    struct String_Value : public Value
    {
        std::string value;
    };
    
    vector<Value *> vector_of_values;
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center