CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2009
    Posts
    166

    how can I convert a vector of vectors to a 1d std::vector?

    Hello all,

    I have a 2d vector like this:

    vector<vector<DWORD> > bits = vector<vector<DWORD> >(100, vector<DWORD>(100));

    I would like to, in code, convert this vector to 1d like this:

    vector<DWORD> 1dBits;

    How is the fastest and best way to accomplish this? Example code appreciated.

    Regards,
    Ellay K.

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: how can I convert a vector of vectors to a 1d std::vector?

    The most straight forward way is to loop and append each element of the 2D vector
    to the 1D vector ...

    Note : I used int instead of DWORD in example below

    Code:
      vector< vector<int> > bits = vector< vector<int> >(100, vector<int>(100)); 
      
      vector<int> bits1d;
      
      bits1d.reserve(100*100);
      
      // or in general, if size of each row can be different
      
      size_t reserve_size = 0;
      
      for (int i=0; i<bits.size(); ++i)
      {
        reserve_size += bits[i].size();
      }
      bits1d.reserve(reserve_size);
        
      for (int i=0; i<bits.size(); ++i)
      {
        const vector<int> & v = bits[i];  // just to make code more readable (note ..  a reference)
        
        bits1d.insert( bits1d.end() , v.begin() , v.end() );
      }
    Last edited by Philip Nicoletti; March 20th, 2012 at 08:40 AM.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: how can I convert a vector of vectors to a 1d std::vector?

    Quote Originally Posted by ekhule View Post
    Hello all,

    I have a 2d vector like this:

    vector<vector<DWORD> > bits = vector<vector<DWORD> >(100, vector<DWORD>(100));

    I would like to, in code, convert this vector to 1d like this:

    vector<DWORD> 1dBits;

    How is the fastest and best way to accomplish this? Example code appreciated.

    Regards,
    Ellay K.
    The fastest way is to define the 2D vector differently in the first place:
    Code:
    vector<DWORD> 1dBits(100*100);
    vector<DWORD*> bits(100);
    for (unsigned i = 0; i < 100; ++i)
        bits[i] = &1dBits[100*i];
    I don't know if that is an option in your case, though, because it does reduce resizing flexibility.

  4. #4
    Join Date
    Mar 2009
    Posts
    166

    Re: how can I convert a vector of vectors to a 1d std::vector?

    Thanks for the replies gentlemen

  5. #5
    Join Date
    Mar 2009
    Posts
    166

    Re: how can I convert a vector of vectors to a 1d std::vector?

    Hey Paul,

    I get this compilation error:

    error C2440: 'initializing' : cannot convert from 'std::vector<_Ty>' to 'const std::vector<_Ty> &'

    on this line:

    const vector<int> & v = bits[i];

    What is wrong?

    Thanks,
    Ellay

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: how can I convert a vector of vectors to a 1d std::vector?

    Quote Originally Posted by ekhule View Post
    Hey Paul,

    I get this compilation error:

    error C2440: 'initializing' : cannot convert from 'std::vector<_Ty>' to 'const std::vector<_Ty> &'
    What is the version of Visual Studio, you're using?

    Also, can you put your code in its full context? What exactly is "bits"?

    This code that Philip has compiles with no issues if placed within a functional block and the header is included:
    Code:
    #include <vector>
    using namespace std;
    
    int main()
    {
      vector< vector<int> > bits = vector< vector<int> >(100, vector<int>(100)); 
        vector<int> bits1d;
      
      bits1d.reserve(100*100);
    
      size_t reserve_size = 0;
      
       for (int i=0; i<bits.size(); ++i)
       {
            reserve_size += bits[i].size();
       }
       bits1d.reserve(reserve_size);
        
       for (int i=0; i<bits.size(); ++i)
       {
           const vector<int> & v = bits[i]; 
           bits1d.insert( bits1d.end() , v.begin() , v.end() );
       }
    }
    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: how can I convert a vector of vectors to a 1d std::vector?

    As I noted in my reply, example used int, not DWORD.
    You just need to make the appropriate changes.

  8. #8
    Join Date
    Mar 2009
    Posts
    166

    Re: how can I convert a vector of vectors to a 1d std::vector?

    hmm ok, so my code looks like this now:

    Code:
    in .h file:
    vector<vector<int> > bits_;
    
    vector<int> bits1d;
    bits1d.reserve(RADARWIDTH*RADARHEIGHT);
    for(unsigned int i=0; i<bits_.size(); ++i)
    {
       const vector<int>& v = bits_[i];
       bits1d.insert(bits1d.end(), v.begin(), v.end());
    }
    ::SetDIBits(dc.m_hDC, bitmap, 0, dyextent, &bits_[0], &bmi_,0);
    for some reason the setting of the bitmap does not work.. it never appears..

    any idea what could be wrong?

  9. #9
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: how can I convert a vector of vectors to a 1d std::vector?

    I don't know if it makes a difference ...
    but why did you change your code from using DWORD to int ?

    Maybe the sign bit is causing problems.

    You should use the types in your original code.

    Also, if your ultimate usage is for this, it is inefficient to use
    a vector<vector> and then convert to a 1D vector. You
    might as well just start with a 1D vector.

  10. #10
    Join Date
    Mar 2009
    Posts
    166

    Re: how can I convert a vector of vectors to a 1d std::vector?

    figured it out.. typo in my code:

    ::SetDIBits(dc.m_hDC, bitmap, 0, dyextent, &bits_[0], &bmi_,0);

    should be:

    ::SetDIBits(dc.m_hDC, bitmap, 0, dyextent, &bits1d[0], &bmi_,0);


    Thanks Phillip!!!

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