c++ - 您可以将 vector<int64> 转换为 vector<uint8> 吗

标签 c++

在 C++ 中,将 int64_t 的 vector 转换为 uint8_t 的正确方法是什么?

我知道我可以这样做:

std::vector<int64_t> int64Vec;
std::vector<uint8_t> uint8Vec(int64Vec.begin(), int64Vec.end());

但我认为创建了一个不必要的拷贝。是否可以转换载体?

原因是 uint8vec 被传递给需要 uint8_t vector 的函数。

最佳答案

Is it possible to cast the vector?

没有。

The reason why is that uint8vec is passed to a function which expects a uint8_t vector.

应该修复的是那个函数。如果该函数改为接受迭代器(当取消引用时产生一个可以转换为 uint8_t 的类型)就不会有问题:

template <typename IT>
void function( IT begin, IT end);

也许为类型添加完整性检查 ITIT 的情况下获得可读的错误消息不取消引用可以转换为 uint8_t 的类型.

关于c++ - 您可以将 vector<int64> 转换为 vector<uint8> 吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59610483/

相关文章:

c++ - C++ 中的快速函数对象?

c++ - Visual Studio C++ 调试器 : No hex dump?

c++ - 如何从 C 调用 C++ 函数?

c++ - OpenGL统一标准缓冲区对齐困惑

c++ - 基于范围的循环中的 const 引用不起作用

c++ - 绑定(bind)到枚举的类成员

c++ - 在 for_each 中使用变换 boost fusion

c++ - weak_ptr 是否与 unique_ptr 一起工作?

c++ - 质数嵌套循环逻辑

c++ - 享元模式和 C++ 模板