简体   繁体   English

std :: vector <char> 进入std :: vector <T> 转换c ++

[英]std::vector<char> into std::vector<T> conversion c++

I want to create a std::vector<T> of length len from serialized data contained in a std::vector<char> of length len * (sizeof T) , without copying data when not necessary (it may not be possible for, eg, align requirements of T ). 我想从长度为len * (sizeof T)std::vector<char>中包含的序列化数据创建长度为lenstd::vector<T> ,在没有必要的情况下不复制数据(对于,例如,对齐要求T )。 In other words I look for an optimization of the following code, operating on the same guarantees: 换句话说,我正在寻找以下代码的优化,并在相同的保证下运行:

template<typename T>
auto copyStuff(std::vector<char>&&input) {
    std::vector<T> result;
    result.resize(input.size()/(sizeof T));
    for (int i=0; i < result.size(); i++) {
        memcpy(&result[i], &input[i * (sizeof T)], sizeof T);
    }
    return result;
}

which 哪一个

  • avoids copying at all when possible 尽可能避免复制
  • otherwise copies all bytes in a single memcpy call if possible 否则,如果可能,在一次memcpy调用中复制所有字节
  • or falls back to the above implementation or equivalent when neither is possible. 或在均无法实现时退回至上述实现或同等方法。

Is there a standard way, or a portable library, to achieve this (or a "good enough" approximation)? 是否有标准方法或可移植库来实现这一目标(或“足够好”的近似值)?

Clarifications: 说明:

  • I need to be able to only assume that T is trivially copiable, and the layout of the input std::vector<char> is simply the concatenation of the memory representation of the item which I want to construct. 我只需要假设T是可微复制的,输入std::vector<char>的布局就是我要构造的项的内存表示形式的串联。
  • I am looking for a way to be certain that I'm not going into undefined or implementation defined behaviour when doing optimizations 我正在寻找一种确定在进行优化时不会进入未定义或实现定义的行为的方法
  • I am willing to consider non-standard container libraries (instead of vector s) if they offer this low overhead type punning. 我愿意考虑非标准容器库(而不是vector ),如果它们提供这种低开销的类型修剪。

It is not possible to get vector of one type from another type without copying. 如果没有复制,则不可能从另一种类型中获得一种类型的向量。

You can avoid one set of copies by constructing the vector of T (of sufficient size) initially, and then writing the serialized data directly onto that vector without the use of an intermediate vector of chars. 通过首先构造T (足够大)的向量,然后不使用chars的中间向量,直接将序列化的数据写入该向量,就可以避免一组副本。

A few caveats: 一些警告:

  • Copying the data directly will mean that the data is not portable to CPU's with different endianness or otherwise different representation of numbers, so this form of serialisation is not appropriate for communication across systems (such as networking, shared files). 直接复制数据将意味着该数据不能以不同的字节序或其他不同的数字表示形式移植到CPU,因此这种形式的序列化不适用于跨系统的通信(例如网络,共享文件)。
  • T must be trivially copyable type. T必须是平凡可复制的类型。

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

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