Re: How to copy vector?
Immortal Nephi wrote:
[.. huge unnecessary quote snipped..]
Victor,
Thank you for your assistance. I have another question.
Please don't quote what isn't necessary to continue the conversation.
> First
variable has 640 elements. Second variable has only 64 elements.
They look like ten 2D images with 64 area of elements.
So, the first one *logically* consists of the ten "areas", but
programmatically simply has 640 elements. The "boundary" between
"areas" is simply *assumed*, not necessarily programmed, correct?
I can't use assignment to copy one out of ten images.
Not directly, no. You can extract part using 'std::copy'.
> Do I need to
use memory address with offset?
No. Just a pair of iterators would do:
std::vector<int> all_images(640); // assuming it's filled in
...
std::vector<int> one_image;
int from = 260, to = from + 64; // whatever your range def is
assert(to <= all_images.size()); // or check and don't copy
std::copy(&all_images[from], &all_images[to],
std::back_inserter(one_image));
Or you can construct your 'one_image' from that range:
std::vector<int> one_image(&all_images[from], &all_images[to]);
> Or I have to use three dimension
array?
I don't know. It might help. But it means you need to redesign your
program. It is up to you.
vector< int > foo()
{
static const int TwoDimensionArray = 10;
static const int Width = 8;
static const int Height = 8;
vector< int > result;
result.resize( TwoDimensionArray * Width * Height );
// or vector< int > result[ TwoDimensionArray ][ Width ][ Height ];
for( int i = 0; i != result.size(); ++i )
result.push_back( i );
return result; // I want to copy only one image with 64 elements with
memory address
// or return result[5][0][0];
You need to extract the result before you can copy it. Here you could
simply do
return vector<int>(&result[Width*Height*5],
&result[Width*Height*(5 + 1)]);
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask