Multi-dimension STL vector
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: Multi-dimension STL vector

  1. #1
    Join Date
    Dec 2003
    Posts
    7

    Multi-dimension STL vector

    Help!
    I am trying to use the STL vector multi-dimenstional (I know how to do 2-d but I want to use 3-d

    This is what I have tried

    vector < vector <vector <int> >> m(3, vector< vector<int> >(4,5));

    I also tried the following:

    vector < vector <vector <int> >> m(3, vector<int> (5, vector<int> (4)));

    I get the similar error as shown above.

    Can you please shed some light?

    Also is there another way to avoid using 3-d vector if I want to use it for the following

    int i,j,k;
    float a[i][j][k]=value;

    off course I would like to vary the size of array dynamically.

    Your help is appreciated.

    AD



    ****************************************************

    c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\vector(357): error C2664: 'std::vector<_Ty>::_Construct_n' : cannot convert parameter 2 from 'int' to 'const std::vector<_Ty> &'
    with
    [
    _Ty=std::vector<int>
    ]
    and
    [
    _Ty=int
    ]

  2. #2
    Join Date
    Apr 1999
    Posts
    27,434
    Use typedefs:
    Code:
    #include <vector>
    // To much work!!
    std::vector < std::vector <std::vector <int> > > 
    m(3,std::vector< std::vector<int> >(4, std::vector<int>(4,0)));
    
    // Easier to maintain
    typedef std::vector<int> Int1D;
    typedef std::vector<Int1D> Int2D;
    typedef std::vector<Int2D> Int3D;
    
    int main()
    {
       Int3D My3DArray(10, Int2D(10, Int1D(10,0)));
    }
    The reason for the typedefs is that you will at some point may need an iterator. When you define the iterator, what's more easier?
    Code:
    std::vector< std::vector< std::vector<int> > >::iterator it;
    OR
    Code:
    Int3D::iterator it;
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Dec 2003
    Posts
    7

    populate a 3-d vector

    How do I populate a 3-d stl vector using a for statement?

    thank you

  4. #4
    Join Date
    Apr 1999
    Posts
    27,434

    Re: populate a 3-d vector

    Originally posted by AfricanDude
    How do I populate a 3-d stl vector using a for statement?

    thank you
    I don't know exactly what you mean. You mean this?
    Code:
    Int3D My3DArray(10, Int2D(10, Int1D(10,0)));
    
    int size1 =  My3DArray.size();
    for ( int i = 0; i < size1; ++i )
    {
        int size2 = My3DArray[i].size();
        for (int j = 0; j < size2; ++j )
        {
            int size3 = My3DArray[i][j].size();
            for ( k = 0; k < size3; ++k )
                 MyArray[i][j][k] = 15;
        }
    }
    If you are talking about how do you size each of the dimensions, then that is a different question.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Dec 2003
    Posts
    7

    populate a 3-d vector

    That is what I did, I wanted to make sure.

    Thank you very much for the help.

    Good Day!
    AD

  6. #6
    Join Date
    Jun 2000
    Location
    austin
    Posts
    101

    Re: Multi-dimension STL vector

    Originally posted by AfricanDude
    vector < vector <vector <int> >> m(3, vector< vector<int> >(4,5));

    vector < vector <vector <int> >> m(3, vector<int> (5, vector<int> (4)));
    >> is interpreted by the compiler as the right shift operator.

  7. #7
    Join Date
    Dec 2003
    Posts
    7

    structure with a vector as a member

    Hello:
    I have this interesting problem...
    I have to use a c-library that contains a function that is defined as follow:

    int func(char * cpData);

    I have been using a structure accually a nested structure as follows

    struct A
    {
    char cString;
    char cAnotherstring;
    }sA;

    struct B
    {
    int iIndex;
    float fData[MAXDATA];

    }sB;

    struct C
    {
    struct A nA;
    struct B nB[8];

    }sStream;


    I can call this structure with now problems
    using
    func((char *) &sStream);

    The problem happenes when

    I wanted to use a vector for the float fData

    such as the struct B is as follows:

    struct B
    {
    int iIndex;
    vector< float> fData;
    } sB;


    when I call the function I getting garbaged data.
    I do take care of the reserving the appropriate data size for the vector
    In fact I can see the data correctly at a point before I used this legecy
    function.


    Is there a way I can convert somehow to c-array which the function understand
    but be able to use the advantage of the vector.

    Your speedy reply wiill be vey much appreciated.

    AD

  8. #8
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: Multi-dimension STL vector

    So basically you need to serialize the data stored in the vector? That won't happen though a cast to char*. All that gives you is the memory-layout of the vector class itself, not the data it is storing. The same thing would happen if you had c-style strings (char*) in your structs. You would see the pointer, but not the string it is pointing to.

    One solution to this is to implement operator >> and << for your structs to enable serialization to a std::ostream. (Or maybe your own serializer class.) Casting pointers like you do now, is something you really, really should not do if you aren't fully aware of the consequences.
    Insert entertaining phrase here

  9. #9
    Join Date
    Jun 2009
    Posts
    2

    Re: Multi-dimension STL vector

    Quick question regarding the 3d vector, which way does it display given how you guys created it?

    Code:
    #include <vector>
    // To much work!!
    std::vector < std::vector <std::vector <int> > > 
    m(3,std::vector< std::vector<int> >(4, std::vector<int>(4,0)));
    
    // Easier to maintain
    typedef std::vector<int> Int1D;
    typedef std::vector<Int1D> Int2D;
    typedef std::vector<Int2D> Int3D;
    
    int main()
    {
       Int3D My3DArray(10, Int2D(10, Int1D(10,0)));
    }
    so lets say:

    1d = [3 4 3]
    2d = [5 6 5]
    3d = [7 8 7]

    Would it display as above? just in a matrix?
    or does it do down.. like:

    [3 5 7]
    [4 6 8]
    [4 5 7]

    Trying to grasp what its doing in code, took a bunch of linear algebra courses but havent applied to code yet.

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Multi-dimension STL vector

    Just decide on an interpretation of the dimensions and stick with it. You can write your own display function however you want.

    I also suggest looking into boost::multi_array.

  11. #11
    Join Date
    Jan 2004
    Location
    DŁsseldorf, Germany
    Posts
    2,401

    Re: Multi-dimension STL vector

    Displaying a 3-dimensional vector on a 2-dimensional monitor is intrinsically difficult, so I would suggest using a 3D graphic library like OpenGL to display it.

    But that's just me...
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  12. #12
    Join Date
    Jun 2009
    Posts
    2

    Re: Multi-dimension STL vector

    Quote Originally Posted by treuss View Post
    Displaying a 3-dimensional vector on a 2-dimensional monitor is intrinsically difficult, so I would suggest using a 3D graphic library like OpenGL to display it.

    But that's just me...
    Yeah, thats what we are trying to do actually, were using glut and C++
    i think how were gonna manage xyz is a single vector for x, single for y and single for z, just to keep it uncomplicated, then the vertex number would just be associated with the position [i] for any info.

    Thanks for the help!

  13. #13
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Multi-dimension STL vector

    >> is interpreted by the compiler as the right shift operator.
    Not necessarily. As with VC 2005, it works! Compiler is smart enough to understand the difference. Yeah, to be on safer side, we should use space in between...
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

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