[英]Can you cast a vector<int64> to a vector<uint8>
In C++ what is the correct way to cast a vector of int64_t
to uint8_t
?在 C++ 中,将
int64_t
向量转换为uint8_t
的正确方法是什么?
I understand I could to this:我明白我可以这样做:
std::vector<int64_t> int64Vec;
std::vector<uint8_t> uint8Vec(int64Vec.begin(), int64Vec.end());
But I think creates an unnecessary copy.但我认为创建了一个不必要的副本。 Is it possible to cast the vector?
是否可以投射向量?
The reason why is that uint8vec
is passed to a function which expects a uint8_t
vector.原因是
uint8vec
被传递给一个需要uint8_t
向量的函数。
Is it possible to cast the vector?
是否可以投射向量?
No.不。
The reason why is that uint8vec is passed to a function which expects a uint8_t vector.
原因是 uint8vec 被传递给一个需要 uint8_t 向量的函数。
What should be fixed is that function.应该修复的是那个函数。 If the function would accept iterators instead (that when dereferenced yield a type that can be converted to
uint8_t
) there would be no issue:如果该函数接受迭代器(当取消引用时产生可以转换为
uint8_t
的类型),就不会有问题:
template <typename IT>
void function( IT begin, IT end);
Perhaps add sanity checks for the type IT
to get readable error messages in case IT
does not dereference to a type that can be converted to uint8_t
.也许为
IT
类型添加健全性检查以获取可读的错误消息,以防IT
没有取消引用可以转换为uint8_t
的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.