简体   繁体   English

向量到向量字节的两倍

[英]Vector of double to a vector bytes

I have a std::vector<double> and need to work with a library the takes a const vector<uint8_t> . 我有一个std::vector<double> ,需要使用需要const vector<uint8_t> I specify what type the data is to the library with an enum. 我用枚举指定数据是什么类型的库。

Is there anyway that I can avoid copying data completely and have the byte vector internally refer to the same data as the double vector? 无论如何,我可以避免完全复制数据并让字节向量在内部引用与双精度向量相同的数据吗? Since the byte vector is const and the double vector won't change during the lifetime of the byte vector, this appears like it would be pretty safe. 由于字节向量是const且双精度向量在字节向量的生存期内不会更改,因此看起来很安全。 There is a lot of data so copying it really isn't an option. 数据很多,因此复制它确实不是一种选择。

If your "byte vector" were actually a vector of bytes then you would have a chance, because you can legally examine pretty much anything as a char array. 如果您的“字节向量”实际上是字节的向量,那么您将有机会,因为您可以合法地检查几乎所有作为char数组的东西。 However, uint32_t s are not bytes and they are certainly not char s. 但是, uint32_t不是字节,并且它们肯定不是char So, no, you basically can't do this without horrible hacky magic whose safety will be entirely implementation dependent. 因此,不,没有可怕的骇客魔法,您基本上无法做到这一点,其安全性将完全取决于实现。

Either way , you can't do it with the vector types: you'd have to cast and pass the result of std::vector::data() , ie a pointer. 无论哪种方式 ,您都不能使用向量类型:必须转换并传递std::vector::data() ,即指针。

Sorry but I have to recommend revisiting your design. 抱歉,但我建议您重新设计。 If the library you're using really takes a vector of integers that is actually supposed to be a vector of doubles, then its developers have put you in an awkward position. 如果您使用的库确实采用了一个整数向量,而该向量实际上应该是double的向量,则其开发人员会将您置于尴尬的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM