c++ - Can a std::string contain embedded nulls? -


For regular C strings, an empty character is a symbol of the end of the '\ 0' data . / P>

What about std :: string , can I have a string with embedded blank characters?

Yes, you tap into your std :: string You can.

example:

  std :: string; S.push_back ('\ 0'); S.push_back ('one'); Emphasize (s.length () == 2);  

Note: std :: string 's c_str () will always add a red letter to the four buffers that have been changed; However, std :: string 's data () can return a returned character buffer to an empty character or not.

Be careful with the operator + =

On the RBS operator + = a char * There is not one thing to use with. It only connects to empty characters.

For example:

  std :: string s = "hello"; S + = "\ 0world"; Emphasize (s.length () == 5);  

Correct method:

  std :: string s = "hello"; S + = std :: string ("\ 0world", 6); Emphasize (s.length () == 11); More common binary data stored to use  

std :: vector

Do this std :: vector to store uncontrolled binary data.

  std :: vector & lt; Char & gt; Buf; Buf.resize (1024); Char * p = & amp; Buf.front (); Perhaps this is more common than  >  and  c_str ()  of  

std :: string Member Coordinator Points come back, so the Memory is not replaceable with & amp; Buf.front () You are free to modify the contents of the buffer directly.


Comments