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.
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() );
}
Re: how can I convert a vector of vectors to a 1d std::vector?
Quote:
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.
Re: how can I convert a vector of vectors to a 1d std::vector?
Thanks for the replies gentlemen :)
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
Re: how can I convert a vector of vectors to a 1d std::vector?
Quote:
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
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.
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?
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.
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!!!