Ah, of course. Using resize() & size() are correct.
Was I at least right about needing to manually add a '\0' to the vector? :)
Printable View
Ah, of course. Using resize() & size() are correct.
Was I at least right about needing to manually add a '\0' to the vector? :)
Cpjust: vec.push_back( '\0' ); // I can't remember if this is required, but it doesn't hurt.
Apparrently its working without the '\0'.
At least i dont have garbage at the end and nothing missing either.
And by the way why would you need to allocate space in a vector?
Arent vectors like strings?
"Actually you could say that a string is a vector<char> A container of chars, or an advanced array of chars. "
Yes, but only if you use the vector's functions to add things to it. If you pass a pointer to the first element of the vector to a function, it won't be calling the vector's functions, it'll just write to it like it's a raw array.
If things like the following work:Quote:
Apparrently its working without the '\0'.
At least i dont have garbage at the end and nothing missing either.
then this can only be because of pure (un)luck. If you are going to treat it as a C-style string, it has to be null-terminated. If you copy from the string to the vector, then string.end() or string.size() won't include the null-terminator (which doesn't have to be there).Code:string s = &v[0];
Thank you, anon.
You sure know how to recognize different types of trees from quite a long way away. :)
I will '\0' terminate it then.
And if i concatenate a vector element to a string like this, do i need to '\0' terminate it too?
Code:int main()
{
string str1 = "Hello ";
string str2 = "World";
vector <char> vec( str2.begin(), str2.end());
str1 += &vec[0];
cout << str1 << endl;
return 0;
}
Thank you Bithub!
Can it be done simpler than this?
Code:
str1 += &vec[0];
str1 += '\0';
That is a contrived example, though. Normally you wouldn't need to add the '\0' to the vector if you do it right.
Earlier there was a discussion about pushing back a null character in cpjust's example (post #6). Pushing it back there is not required and doesn't do anything unless you use the vector as a C-style string later on, but you would normally just use c_str() for that.
When that code is changed to use resize, that actually adds 100 null characters anyway, so filling the vector with data from the old function would only cause a problem if all 100 characters were filled with string data. Then you might get garbage characters at the end. One solution is to resize to 100 (or whatever) and only pass 99 characters to your old function. Another is to assign the vector to the string.
Code:std::string str = "Hello World";
std::vector<char> vec1( str.begin(), str.end() );
// You would want push_back('\0') here if using the vector as a char*
// but that's not usually necessary, use c_str() on the string instead
Code:std::vector<char> vec2( 100 ); // Initialize with 100 null characters
old_c_function( &vec[0], vec.size() );
// Now put it back into the string.
str.assign(vec.begin(), vec.end());
// No null needed, we used assign.
Code:std::vector<char> vec2( 100 ); // Initialize with 100 null characters
old_c_function( &vec[0], vec.size()-1 );
// This is a bit more dangerous, I prefer assign.
str = &vec[0];
// No null needed, only filled size()-1 characters, and the last character was initialized to null.
Thanks Daved!
You are revealing details about vectors that doesnt really explained anywhere. Interesting.
std::string does not require null termination. The question of how to properly "null terminate" the vector is moot.
Convert vector of char to string:
Convert string to vector of char:Code:std::string foo( vec.begin(), vec.end() );
And yes, when you call c_str() on the resulting string, it will be null terminated automatically. std::string is nothing like a C-string.Code:std::vector< char > foo( str.begin(), str.end() );
If you just wanted a C string for reading, you'd use string::c_str(). The reason for using a vector is to pass it to a C function for writing into a C string, and for that you'd need to add a '\0' char to the end (assuming it's going to append instead of just overwriting the string).