如何将标准::字符串转换为标准::矢量<uint8_t>?

How to convert std::string to std::vector<uint8_t>?

本文关键字:标准 uint8 gt lt 矢量 字符串 转换      更新时间:2023-10-16

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

我假设向量代表某种字节数组。这是对的吗?那么如何将std::string转换为std::vector<uint8_t>呢?

std::vector有一个构造函数,仅用于此目的:

std::string str;
std::vector<uint8_t> vec(str.begin(), str.end());

除了DeiDei的答案之外,如果向量已经构造好,你可以执行以下操作:

std::string str;
std::vector<uint8_t> vec;
...
vec.assign(str.begin(), str.end());