Jump to content
  • Advertisement
Sign in to follow this  
johnnyBravo

Is there a method of copying a std::vector? (c++)

This topic is 4503 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi in c++,I've got a: vector<vector<bool> > And I was wondering if there is a quick way of making a copy of it, eg instead of looping through the two dimensional array equalling each element. Thanks

Share this post


Link to post
Share on other sites
Advertisement
std::vector<std::vector<bool> > theCopy = theOriginal;
or
std::vector<std::vector<bool> > theCopy(theOriginal);

The beauty here is that everything automagically takes care of itself.

Share this post


Link to post
Share on other sites
... and always remember that if it is vector<A>, and the class A has pointers, the pointers in both the copy and the original will still point to the same addresses.

Share this post


Link to post
Share on other sites
std::copy

std::vector<int> v1( 5, 3 );
std::vector<int> v2( v1.size() );
std::copy( v1.begin(), v1.end(), v2.begin() );


std::vector<int> v1( 5, 3 );
std::vector<int> v2;
std::copy( v1.begin(), v1.end(), std::back_inserter( v2 ) );

std::copy is more flexible, but zipster's methods are better if you're just copying a vector to a vector.

Share this post


Link to post
Share on other sites
Quote:
Original post by babbling
... and always remember that if it is vector<A>, and the class A has pointers, the pointers in both the copy and the original will still point to the same addresses.

Nope. Copy constructors and assignment operators are called appropriately, so the vector copy will behave exactly how you would expect.

CM

Share this post


Link to post
Share on other sites
...assuming that the copy constructor, destructor and assignment operator are appropriately defined. The compiler-generated default versions almost certainly won't work correctly.

Share this post


Link to post
Share on other sites
There are people here that will have a heart attack for me saying this, but you can use memcpy on vectors for POD types. Something like:

vector<int> dst(src.size());
memcpy( &dst[0], &src[0], src.size() * sizeof( int ) );

I wouldn't recommend this except for the most extreme performance requirements, and of course it's not guaranteed to work for any given implementation, but it should work for any of the ones that are out there.

Share this post


Link to post
Share on other sites
Quote:
Original post by krum
There are people here that will have a heart attack for me saying this, but you can use memcpy on vectors for POD types. Something like:

vector<int> dst(src.size());
memcpy( &dst[0], &src[0], src.size() * sizeof( int ) );

I wouldn't recommend this except for the most extreme performance requirements, and of course it's not guaranteed to work for any given implementation, but it should work for any of the ones that are out there.


It's probabably implemented simliarly to that when possible, why break encapsulation?

Share this post


Link to post
Share on other sites
Quote:
Original post by wild_pointer
Quote:
Original post by krum
There are people here that will have a heart attack for me saying this, but you can use memcpy on vectors for POD types. Something like:

vector<int> dst(src.size());
memcpy( &dst[0], &src[0], src.size() * sizeof( int ) );

I wouldn't recommend this except for the most extreme performance requirements, and of course it's not guaranteed to work for any given implementation, but it should work for any of the ones that are out there.


It's probabably implemented simliarly to that when possible, why break encapsulation?


I agree, but I think that at the end of the day this is what the OP wanted to know. :)

Share this post


Link to post
Share on other sites
Many standard library implementations nowadays use template specializations with compiler intrinsics to check whether a type to be copied is POD, and utilize memcpy whenever possible.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!