相关疑难解决方法(0)

如何在需要旧式unsigned char的地方使用新的std :: byte类型?

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)

我试图表演天真的演员,但这也没有帮助.那么,如果它被设计为有用,它在旧的上下文中是否真的有用?我看到的唯一方法是 …

c++ vector c++17

31
推荐指数
1
解决办法
1万
查看次数

如何将std :: string转换为std :: vector <uint8_t>?

在Google Play游戏服务上保存游戏所需的数据格式为:std::vector<uint8_t>在"数据格式"下指定:https: //developers.google.com/games/services/cpp/savedgames

我假设向量代表某种字节数组.那是对的吗 ?那么如何转换std::stringstd::vector<uint8_t>

c++ c++11

13
推荐指数
2
解决办法
1万
查看次数

std ::在std :: string和std :: vector <unsigned char>之间移动

我正在使用2个库.一个接受并返回std::strings而另一个使用std::vector<unsigned char>s.

这将是很好的,如果我可以从偷底层阵列std::stringstd::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)

c++ string vector std unsigned-char

12
推荐指数
2
解决办法
2万
查看次数

标签 统计

c++ ×3

vector ×2

c++11 ×1

c++17 ×1

std ×1

string ×1

unsigned-char ×1