I
Immortal Nephi
I create two vector variables. First variable: data is initialized
with integers. Second variable: copyData is empty. Both have same
sizes.
How do I write correct code to copy all elements from data to
copyData? 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?
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;
}
with integers. Second variable: copyData is empty. Both have same
sizes.
How do I write correct code to copy all elements from data to
copyData? 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?
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;
}