Basically, can I use the memcpy( ) method when copying an integer from a vector to an integer array( and vice-versa )?
Here is what I mean:
1 2 3 4 5 6 7 8 9
// The vector which contains one integer with the value of seven.
vector < int > vIntVector( 1, 7 );
// The integer array which contains one integer with the value of zero.
int *pnIntArray( newint[ 1 ] );
pnIntArray[ 0 ] = 0;
// Copy the value from the vector to the integer array.
memcpy( pnIntArray, &vIntVector, sizeof( vIntVector ) );
You can.
That's limited to int and other PODs, though.
Edit: but not like that. sizeof(vIntVector) certainly does not give the result you want and copying from &vIntVector is incorrect as well. The following works:
note you should prefer std::copy over memcpy. Especially when dealing with STL containers:
1 2 3 4 5
// basically it works like this:
std::copy( src, src + size, dest );
// so, you would do this:
std::copy( pnIntArray, pnIntArray + 1, vIntVector.begin() );
in this case, I think the assign member function of the vector itself would be the best choice
atlease use copy rather than memcpy
stl has its own coding style
when you work with stl, you better follow the way stl should be