c++ – fastest way to convert a std::vector to another std::vector – Stack Overflow

What is the fastest way (if there is any other) to convert a std::vector from one datatype to another (with the idea to save space)? For example: we obviously assume that the first vector only contains 0s and 1s. Copying element by element is highly inefficient in case of a really large vector. Conditional question: If you think there is no way to do it faster, is there a complex datatype which actually allows fast conversion from one datatype to another? A std::vector<,bool>, is... not. std::vector has a specialization for the use of the type bool, which causes certain changes in the vector. Namely, it stops acting like a std::vector. There are certain things that the standard guarantees you can do with a std::vector. And vector<,bool>, violates those guarantees. So you should be very careful about using them. Anyway, I'm going to pretend you said vector<,int>, instead of vector<,bool>,, as the latter really complicates things. Vector casting of the type you want needs to be done carefully to be efficient. The biggest problem you'll have for simple types is not doing this: That's bad. That will allocate a buffer of the proper size, but it will also fill it with data. Namely, default-constructed ints (int()). This reserves capacity equal to the original vector, but it also ensures that no default construction takes place. You can now push_back to your hearts content, knowing that you will never cause reallocation in your new vector. There's no way to avoid the copy, since a std::vector<,T>, is a distinct type from std::vector<,U>,, and there's no way for them to share the memory. Other than that, it depends on how the data is mapped. If the mapping corresponds to an implicit conversion (e.g. unsigned short to bool), then simply creating a new vector using the begin and end iterators from the old will do the trick: If the mapping isn't just an implicit conversion (and this includes cases where you want to verify things, e.g. that the unsigned short does contain only 0 or 1), then it gets more complicated. The obvious solution would be to use std::transform: (Note that I'm just using this transformation function as an example. If the only thing which distinguishes the transformation function from an implicit conversion is the verification, it might be faster to verify all of the values in oldV first, using std::for_each, and then use the two iterator constructor above.) Depending on the cost of default constructing the target type, it may be faster to create the new vector with the correct size, then overwrite it: In many ways, this is the solution I prefer, depending on how boost::transform_iterator has been implemented, it could also be the fastest. The fastest way to do it is to not do it. For example, if you know in advance that your items only need a byte for storage, use a byte-size vector to begin with. You'll find it difficult to find a faster way than that :-) If that's not possible, then just absorb the cost of the conversion. Even if it's a little slow (and that's by no means certain, see Nicol's excellent answer for details), it's still necessary. If it wasn't, you would just leave it in the larger-type vector. First, a warning: Don't do what I'm about to suggest. It's dangerous and must never be done. That said, if you just have to squeeze out a tiny bit more performance No Matter What... The vector must contain plain-old-data. If your type has pointers, or uses a destructor, or needs an operator = to copy correctly ... do not do this. The sizeof() both vector's contained types must be the same. That is, vector<, A >, can copy from vector<, B >, only if sizeof(A) == sizeof(B). This does a very fast, block copy of the memory contained in vector b, directly smashing whatever data you have in vector a. It doesn't call constructors, it doesn't do any safety checking, and it's much faster than any of the other methods given here. An optimizing compiler should be able to match the speed of this in theory, but unless you're using an unusually good one, it won't (I checked with Visual C++ a few years ago, and it wasn't even close). Also, given these constraints, you could forcibly (via void *) cast one vector type to the other and swap them -- I had a code sample for that, but it started oozing ectoplasm on my screen, so I deleted it. Copying element by element is not highly inefficient. std::vector provides constant access time to any of its elements, hence the operation will be O(n) overall. You will not notice it. Source.


Яндекс.Метрика Рейтинг@Mail.ru Free Web Counter
page counter
Last Modified: May 27, 2014 @ 12:00 am