Karl Ebener wrote:
> Okay, this is my test program.
My guess is that std::string's functions (including constructors) that take
a C-Style string as an argument, *do* treat it as a C-style (i.e.
null-terminated) string.
Makes sense, doesn't it? You don't want
char s[15] = "sth";
string s1(s);
to allocate 11 extra null characters in s1 for no reason
If, OTOH, you put a '\0' in an std::string, it will not be treated as a
terminating character.
Check out this example to see what I mean:
#include <iostream>
#include <string>
int main(){
std::string s("abc\0abc\0");
std::cout<<s.length()<<std::endl; //prints 3, not 9
std::string s2;
s2.push_back('a');
s2.push_back('\0');
s2.push_back('b');
std::cout<<s2.length()<<std::endl; //prints 3, not 1
}
Note: c_string() will return a const char *, which means that the string
returned will always stop at the first null byte, for any code that cares
about it (e.g. strlen or strcpy). Better use a vector<char> if you want
byte semantics.