What is the correct C ++ 11 way to extract a character set from a string stream without using boost?
I want to do this without copying, if possible, because where it is used is in the critical data loop. It seems that std :: string does not allow direct access to data.
For example, the code below executes a copy of a substring from a string stream:
inline std::string left(std::stringstream ss, uint32_t count) { char* buffer = new char[count]; ss.get(buffer, count); std::string str(buffer);
- Should I use char * buffer according to C ++ 11?
- How do I make a second copy?
I understand that vectors initialize every character, so I want to avoid this.
In addition, this needs to be passed to a function that takes const char *, so now after this run I have to do .c_str (). Does it also make a copy?
It would be nice to be able to pass const char *, but that seems to contradict the "correct" C ++ 11 style.
To understand what I'm trying to do, here is the "efficient" that I want to use for it:
fprintf( stderr, "Data: [%s]...", left(ststream, 255) );
But C ++ 11 forces:
fprintf( stderr, "Data: [%s]...", left(str_data, 255).c_str() );
How many instances of this line can I make here?
How can I reduce it to just one copy from a string?