19

I have a pointer to a vector of type uint8.

How would I take this pointer and convert the data in the vector into a full string representative of its content?

4

3 回答 3

33

您可以std::string使用从以下获得的序列初始化std::vector<uint8_t>

std::string str(v->begin(), v->end());

没有必要玩任何技巧来检查是否std::vector<uint8_t>为空:如果是,则范围将为空。但是,您可能想要检查指针是否v为空。以上要求它指向一个有效的对象。

于 2013-07-31T23:39:22.740 回答
6

对于那些希望在声明字符串进行转换的人,可以使用 std::string::assign(),例如:

std::string str;
std::vector<uint8_t> v;
str.assign(v.begin(), v.end());
于 2017-01-13T15:12:26.443 回答
0
vector<uint8_t> *p;
string str(
    p && !p->empty() ? &*p->begin()             : NULL,
    p && !p->empty() ? &*p->begin() + p->size() : NULL);
于 2013-07-31T23:32:35.943 回答