views:

2465

answers:

7

If I want to construct a std::string with a line like:

std::string my_string("a\0b");

Where i want to have three characters in the resulting string (a, null, b), I only get one. What is the proper syntax?

+4  A: 

I have no idea why you'd want to do such a thing, but try this:

std::string my_string("a\0b", 3);
17 of 26
What are your concerns for doing this? Are you questioning the need to store "a\0b" ever? or questioning the use of a std::string for such storage? If the latter, what do you suggest as an alternative?
Anthony Cramp
I'm questioning why you'd want a string with a null in the middle of it.
17 of 26
Because people have to work with binary data sometimes?
Constantin
A: 

Almost all implementations of std::strings are null-terminated, so you probably shouldn't do this. Note that "a\0b" is actually four characters long because of the automatic null terminator (a, null, b, null). If you really want to do this and break std::string's contract, you can do:

std::string s("aab");
s.at(1) = '\0';

but if you do, all your friends will laugh at you, you will never find true happiness.

Jurney
std::string is NOT required to be NULL terminated.
Martin York
It's not required to, but in almost all implementations, it is, probably because of the need for the c_str() accessor to provide you with the null terminated equivalent.
Jurney
For effeciency a null character _may_ be kept on the back of the data buffer. But none of the operations (ie methods) on a string use this knowledge or are affected by a string containing a NULL character. The NULL character will be manipulated in exactly the same way as any other character.
Martin York
+1  A: 

The following will work...

std::string s;
s.push_back('a');
s.push_back('\0');
s.push_back('b');
Andrew Stein
You have to use parentheses insted of the square brackets.
jk
jk, thanks, I have fixed it up...
Andrew Stein
+7  A: 

If you are doing manipulation like you would with a c-style string (array of chars) consider using

std::vector<char>

You have more freedom to treat it like an array in the same manner you would treat a c-string. You can use copy() to copy into a string:

std::vector<char> vec(100)
strncpy(&vec[0], "blah blah blah", 100);
std::string vecAsStr( vec.begin(), vec.end());

and you can use it in many of the same places you can use c-strings

printf("%s" &vec[0])
vec[10] = '\0';
vec[11] = 'b';

Naturally, however, you suffer from the same problems as c-strings. You may forget your null terminal or write past the allocated space.

Doug T.
+1  A: 

Better to use std::vector<char> if this question isn't just for educational purposes.

Harold Ekstrom
+21  A: 

The problem is the std::string constructor that takes a const char* assumes the input is a C string. C strings are NULL terminated and thus parsing stops when it reaches the NULL.

To compensate for this you need to use the constructor that builds the string from a char array (not a C-String). This takes two parameters a pointer to the array and a length:

std::string   x("pq\0rs");   // Two characters because input assumed to be C-String
std::string   x("pq\0rs",5); // 5 Characters as the input is now a char array with 5 characters.

Note: C++ std::string is NOT NULL terminated (as suggested in other posts). Though you can extract a pointer to an internal buffer that contains a C-String with the method c_str().

Also check out Doug .T below about using a vector<char>

Martin York
A: 

I know it is a long time this question has been asked. But for anyone who is having a similar problem might be interested in the following code.

CComBSTR(20,"mystring1\0mystring2\0")
Dil09