Is there a method of copying a std::vector? (c++)
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
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.
or
std::vector<std::vector<bool> > theCopy(theOriginal);
The beauty here is that everything automagically takes care of itself.
... 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.
std::copy
std::copy is more flexible, but zipster's methods are better if you're just copying a vector to a vector.
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.
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
...assuming that the copy constructor, destructor and assignment operator are appropriately defined. The compiler-generated default versions almost certainly won't work correctly.
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.
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.
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?
Quote:Original post by wild_pointerQuote: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. :)
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement