vector< int > foo()
{
int numData = 1;
vector< int > result;
result.resize( 100 );
for( vector< int >::iterator it = result.begin();
it != result.end(); ++it )
*it = numData++;
return result;
}
int main()
{
vector< int > copyData;
copyData.resize( 100 );
copyData = foo();
return 0;
}
'copyData' contains 100 zeroes.
> How do I write correct code to copy all elements from data to
> copyData?
Just an assignment (for example), as you do below.
> I don�t have to go through iterator loop to copy each
> element.
> I want to know that vector data should be auto storage in the global
> function�s stack frame. Then, the return type will be vector to copy
> all elements.
> If result size is different and is more than 100 elements, will
> copyData be automatically resized?
Yes.
> vector< int > foo()
> {
> int numData = 1;
> vector< int > result;
> result.resize( 100 );
>
> for( vector< int >::iterator it = result.begin();
> it != result.end(); ++it )
> *it = numData++;
>
> return result;
> }
You could write this more easily as
vector<int> foo()
{
vector<int> result;
for( int i = 1; i <= 100; ++i )
{
result.push_back( i );
}
return result;
}
> int main()
> {
> vector< int > copyData;
> copyData.resize( 100 );
This size specification is not necessary.
> copyData = foo();
>
> return 0;
And this 'return' isn't necessary either. 'main' returns 0 by default. :-)
> }
Cheers & hth.,
- Alf
It is customary to describe what *type* your variables have. 'vector'
(or, rather, 'std::vector') is a template, which has to be instantiated
with a type (at least).
> How do I write correct code to copy all elements from data to
> copyData?
How do you copy a 'double'? You assign it. What about a 'bool'? You
assign it. Why do you think it's different with 'std::vector'? It's
not. You simply assign it.
> I don�t have to go through iterator loop to copy each
> element.
No, you don't.
> I want to know that vector data should be auto storage in the global
> function�s stack frame.
Huh?
> Then, the return type will be vector to copy
> all elements.
> If result size is different and is more than 100 elements, will
> copyData be automatically resized?
Uh... Assignment takes care of everything, if that's what you're asking.
>
> vector< int > foo()
> {
> int numData = 1;
> vector< int > result;
> result.resize( 100 );
>
> for( vector< int >::iterator it = result.begin();
> it != result.end(); ++it )
> *it = numData++;
>
> return result;
> }
Looks good. Or, you could simply do
int numData = 1;
vector<int> result;
for (int i = 0; i < 100; i++)
result.push_back(numData++);
(or drop 'numData' altogether and push 'i').
>
> int main()
> {
> vector< int > copyData;
> copyData.resize( 100 );
No need to resize.
>
> copyData = foo();
That's just fine. Or, you could simply initialise it with the return
value of 'foo()':
vector<int> copyData = foo();
>
> return 0;
> }
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thank you for your assistance. I have another question. First
variable has 640 elements. Second variable has only 64 elements.
They look like ten 2D images with 64 area of elements.
I can't use assignment to copy one out of ten images. Do I need to
use memory address with offset? Or I have to use three dimension
array?
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];
}
> 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)]);
[...]
> > int main()
> > {
> > vector< int > copyData;
> > copyData.resize( 100 );
> This size specification is not necessary.
> > copyData = foo();
> > return 0;
> And this 'return' isn't necessary either. 'main' returns 0 by
> default. :-)
It's not necessary according to the formal rules of the
language, but it's necessary for readability and
maintainability. (Not to mention the fact that I've never yet
written a real program where return 0 was acceptable. There's
always some sort of possible error.)
> > }
--
James Kanze
He he, your second sentence trashes, trumps and generally stomps on the first
one. When the return is misleading, as it is here, and you do mention that, :-),
how can it help maintainability instead of hindering maintainability?
Cheers,
- Alf
[...]
> >>> return 0;
> >> And this 'return' isn't necessary either. 'main' returns 0 by
> >> default. :-)
> > It's not necessary according to the formal rules of the
> > language, but it's necessary for readability and
> > maintainability. (Not to mention the fact that I've never
> > yet written a real program where return 0 was acceptable.
> > There's always some sort of possible error.)
> He he, your second sentence trashes, trumps and generally
> stomps on the first one. When the return is misleading, as it
> is here, and you do mention that, :-), how can it help
> maintainability instead of hindering maintainability?
Because we're writing simple examples here; the "return 0"
serves as a placeholder for the "return whatever;" that will be
found in the final program. In general, if a function returns
an int, it should have a return statement; making an exception
for main doesn't buy you anything but confusing.
--
James Kanze