extern const map<string, vector<unsigned char>> my_map;
// get const reference, no copying
const vector<unsigned char> &data = my_map.at("my_key");
// also no copy?
istringstream iss;
iss.rdbuf()->pubsetbuf((char *)data.data(), data.size());
How does the last line work? I saw it on multiple SO answers on how to create a istringstream
without copying.
If I understand this correctly, data
is a const reference to the element of the map whose key is "my_key". And since at()
returns by const reference as well, the values of the vector weren't copied. But since it's a const reference, then I can't change the elements of the vector.
data()
returns a const unsigned char*
, which I cast to char*
. Does this mean that the elements of the array can now be changed? (since its a pointer to a non-const type). Or did the vector get copied when I casted it?