Please help! -- C-style 2D array containing std::vector
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Please help! -- C-style 2D array containing std::vector

  1. #1
    Join Date
    Jul 2009
    Posts
    25

    Please help! -- C-style 2D array containing std::vector

    I know it's weird but I would like to create a 2D c-style array. Each array element stores a vector of int. The result will produce 3D array of int.

    I can compile my code but it seems like whenever I add some data into the vector and, later I want to display it, there is no data in the vector!!!

    Please let me know what I did wrong. Thank you very much.

    typedef std::vector<int> VEC_INT;

    // Create array of vector
    VEC_INT ** ppSubRegion;
    VEC_INT * pSubRegion;
    ppSubRegion = (VEC_INT **) malloc(SUBPATTERN * sizeof(VEC_INT *));
    pSubRegion = (VEC_INT *) malloc(SUBPATTERN * SUBPATTERN * sizeof(VEC_INT));

    for (int i = 0; i < SUBPATTERN; i++)
    {
    ppSubRegion[i] = &pSubRegion[i*SUBPATTERN];
    }

    // Add numbers into the vector according to some criteria. There is no error!
    ((VEC_INT) ppSubRegion[nRow][nCol]).push_back(iNumber);

    // After display, it shows that the vector contains nothing.
    (VEC_INT)(ppSubRegion[r][c]).size() <----- return 0!

    Thank you very much
    Last edited by a_sonhye; November 13th, 2009 at 05:49 AM. Reason: For more clearer

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

    Re: Please help! -- C-style 2D array containing std::vector

    You are not creating any vectors at all with malloc.
    Unless you are interfacing with some third party library that requires a parameter created with malloc, there is never any need to use it in C++ code.
    "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

  3. #3
    Join Date
    Jul 2009
    Posts
    25

    Re: Please help! -- C-style 2D array containing std::vector

    My vector is just stl vector.

    typedef vector<int> VEC_INT;

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

    Re: Please help! -- C-style 2D array containing std::vector

    Why not this?

    Code:
    typedef vector<int> VEC_INT;
    
    VEC_INT subRegion[SUBPATTERN][SUBPATTERN];
    
    subRegion[nRow][nCol].push_back(iNumber);
    
    size_t size = subRegion[nRow][nCol].size();
    or

    Code:
    vector<vector<VEC_INT>> subRegion(SUBPATTERN, vector<VEC_INT>(SUBPATTERN));
    
    subRegion[nRow][nCol].push_back(1);
    
    size_t size = subRegion[nRow][nCol].size();
    or some variation thereof?
    "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

  5. #5
    Join Date
    Jul 2009
    Posts
    25

    Re: Please help! -- C-style 2D array containing std::vector

    Hi, JohnW. Thank you very much.

    I'm new for STL.
    And my previous code was programmed that way but at that time, I use the struct below so the code work fine.
    But now, I replace this struct with std::vector so I think i will work the same.

    struct TEXTURE_FEATURES
    {
    double textureFeature[NUM_TEXTURES];
    };

    Also, I need 3D array not 2D array.
    That's why I declare 2D-array and each element of array storing vector.
    Last edited by a_sonhye; November 13th, 2009 at 06:09 AM.

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,694

    Re: Please help! -- C-style 2D array containing std::vector

    For explain further...

    vector<vector<VEC_INT>>

    A vector of vector of vector<int> or 3D vector.


    subRegion(SUBPATTERN, vector<VEC_INT>(SUBPATTERN));

    Calls the row vector constructor, with SUBPATTERN elements in the row,
    each with a value of vector<VEC_INT>(SUBPATTERN)



    vector<VEC_INT>(SUBPATTERN)

    Constructs a column, with SUBPATTERN elements in the column,
    each with a value of VEC_INT (empty int vector).
    "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

  7. #7
    Join Date
    Jul 2009
    Posts
    25

    Re: Please help! -- C-style 2D array containing std::vector

    Thank you very much.

    I got it after copying the code and compile it. Now it's more clear to me. But, can I ask one more question? Just for my curious.

    From my code at the top most post, how can I assign a vector to my 2D pointer, ppSubRegion[nRow][nCol]???

    Thank you very much.

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

    Re: Please help! -- C-style 2D array containing std::vector

    Well, first off, be absolutely sure that assigning a vector is really what you want to do. It can be an expensive operation. If you need to move a vector from one place to another, it's almost always cheaper to swap it with the one that's already there instead. Of course, if you need two copies of the vector then that won't work.

    Depending on which you actually want:
    Code:
    VEC_INT v;
    
    ppSubRegion[nRow][nCol] = v;
    ppSubRegion[nRow][nCol].swap(v);

  9. #9
    Join Date
    Jul 2009
    Posts
    25

    Re: Please help! -- C-style 2D array containing std::vector

    Hello, Lindley.

    Thank you. I tried it and it work. Thanks ^^;;

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center