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

Threaded View

  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 06:49 AM. Reason: For more clearer

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured