最快的方式将std :: vector转换为另一个std :: vector [英] fastest way to convert a std::vector to another std::vector

查看:674
本文介绍了最快的方式将std :: vector转换为另一个std :: vector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是最快的方式(如果有任何其他)将std :: vector从一种数据类型转换到另一种(以保存空间的想法)?例如:

  std :: vector< unsigned short> ----> std :: vector< bool> 

我们显然假设第一个向量只包含0和1。在一个真正大的矢量的情况下逐个元素地复制是非常低效的。



条件问题:
如果你认为没有办法更快,是否有一个复杂的数据类型,实际上允许从一种数据类型快速转换到另一种?

解决方案

  std :: vector< bool& 

停止。



A std :: vector< bool> 是...不是。 std :: vector 具有使用 bool 类型的特殊化,这会导致 vector 。也就是说,它停止像 std :: vector



的行为。使用 std :: vector 。并且向量< bool> 违反了这些保证。所以你应该

非常小心使用它们。



无论如何,我要假装你说

>

在一个非常大的向量的情况下,逐个元素复制是非常低效的。


只有当你做错了。



你希望需要仔细地做,以有效率。你对简单类型最大的问题不是这样:

  std :: vector< int> newVec(oldVec.size()); 

这很糟糕。这将分配一个适当大小的缓冲区,但它也将 填充它的数据。即,默认构造 int s( int())。



相反,您应该这样做:

  std :: vector< int& newVec; 
newVec.reserve(oldVec.size());

这保留了与原始向量相等的容量,但它也确保不会发生默认构造。您现在可以 push_back 加入您的内容,知道您将不会在新的向量中重新分配。



您可以在旧向量中循环每个条目,根据需要进行转换。


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:

std::vector<unsigned short> ----> std::vector<bool> 

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?

解决方案

std::vector<bool> 

Stop.

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.

Copying element by element is highly inefficient in case of a really large vector.

Only if you do it wrong.

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:

std::vector<int> newVec(oldVec.size());

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()).

Instead, you should do this:

std::vector<int> newVec;
newVec.reserve(oldVec.size());

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.

From there, you can just loop over each entry in the old vector, doing the conversion as needed.

这篇关于最快的方式将std :: vector转换为另一个std :: vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆