如何将std :: string转换为std :: vector ?

问题描述 投票:9回答:2

在Google Play游戏服务上保存游戏所需的数据格式为:std::vector<uint8_t>,如“数据格式”中所述:https://developers.google.com/games/services/cpp/savedgames

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

c++ c++11
2个回答
22
投票

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

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

2
投票

添加到DeiDei的答案,如果已经构造了向量,则可以执行以下操作:

std::string str;
std::vector<uint8_t> vec;
vec.assign(str.begin(), str.end());
© www.soinside.com 2019 - 2024. All rights reserved.