std::byte是C++ 17中的新类型,它是作为enum class byte : unsigned char.如果没有适当的转换,这将无法使用它.所以,我为这种类型的向量做了一个别名来表示一个字节数组:
using Bytes = std::vector<std::byte>;
Run Code Online (Sandbox Code Playgroud)
但是,它不可能在旧式中使用它:接受它作为参数的函数失败,因为这种类型不能轻易转换为旧std::vector<unsigned char>类型,例如,zipper库的用法:
/resourcecache/pakfile.cpp: In member function 'utils::Bytes resourcecache::PakFile::readFile(const string&)':
/resourcecache/pakfile.cpp:48:52: error: no matching function for call to 'zipper::Unzipper::extractEntryToMemory(const string&, utils::Bytes&)'
unzipper_->extractEntryToMemory(fileName, bytes);
^
In file included from /resourcecache/pakfile.hpp:13:0,
from /resourcecache/pakfile.cpp:1:
/projects/linux/../../thirdparty/zipper/zipper/unzipper.h:31:10: note: candidate: bool zipper::Unzipper::extractEntryToMemory(const string&, std::vector<unsigned char>&)
bool extractEntryToMemory(const std::string& name, std::vector<unsigned char>& vec);
^~~~~~~~~~~~~~~~~~~~
/projects/linux/../../thirdparty/zipper/zipper/unzipper.h:31:10: note: no known conversion for argument 2 from 'utils::Bytes {aka std::vector<std::byte>}' to 'std::vector<unsigned char>&'
Run Code Online (Sandbox Code Playgroud)
我试图表演天真的演员,但这也没有帮助.那么,如果它被设计为有用,它在旧的上下文中是否真的有用?我看到的唯一方法是 …
在Google Play游戏服务上保存游戏所需的数据格式为:std::vector<uint8_t>在"数据格式"下指定:https:
//developers.google.com/games/services/cpp/savedgames
我假设向量代表某种字节数组.那是对的吗 ?那么如何转换std::string为std::vector<uint8_t>?
我正在使用2个库.一个接受并返回std::strings而另一个使用std::vector<unsigned char>s.
这将是很好的,如果我可以从偷底层阵列std::string和std::vector<unsigned char>并能够将其移动到对方没有过多的复制.
ATM我使用的东西如下:
const unsigned char* raw_memory =
reinterpret_cast<const unsigned char*>(string_value.c_str()),
std::vector<unsigned char>(raw_memory, raw_memory + string_value.size();
Run Code Online (Sandbox Code Playgroud)
另一种方式:
std::string(
reinterpret_cast<const char*>(&vector_value[0]),
vector_value.size());
Run Code Online (Sandbox Code Playgroud)
能够定义一个以下内容会好得多:
std::string move_into(std::vector<unsigned char>&&);
std::vector<unsigned char> move_into(std::string&&);
Run Code Online (Sandbox Code Playgroud)