|
-
March 20th, 2012, 06:44 AM
#1
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.
-
March 20th, 2012, 07:14 AM
#2
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.
-
March 20th, 2012, 09:26 AM
#3
Re: how can I convert a vector of vectors to a 1d std::vector?
 Originally Posted by ekhule
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.
-
March 20th, 2012, 01:18 PM
#4
Re: how can I convert a vector of vectors to a 1d std::vector?
Thanks for the replies gentlemen
-
March 20th, 2012, 01:33 PM
#5
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
-
March 20th, 2012, 01:54 PM
#6
Re: how can I convert a vector of vectors to a 1d std::vector?
 Originally Posted by ekhule
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
-
March 20th, 2012, 03:53 PM
#7
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.
-
March 21st, 2012, 06:29 AM
#8
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?
-
March 21st, 2012, 08:05 AM
#9
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.
-
March 21st, 2012, 09:08 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|