简体   繁体   中英

Convert std::vector<uint8_t> to std::string_view

I have some data of type std::vector<uint8_t> . I would like to interpret it as a string and do some sub-string check on it.

It can be done by converting it to std::string which will cause copying the data. Is it possible to somehow convert it to std::string_view and do search on it to avoid copying it.

Provided that char is 8 bits on your system (most systems are), you can simply reinterpret_cast the vector 's data to char* and make a view from that, eg:

std::vector<uint8_t> data;
...
std::string_view sv(reinterpret_cast<char*>(data.data()), data.size());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM