![]() |
| | #16 |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,120
| Was I at least right about needing to manually add a '\0' to the vector?
__________________ "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008 |
| cpjust is offline | |
| | #17 |
| Registered User Join Date: Dec 2007 Location: France
Posts: 396
| 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. "
__________________ Using Code::Blocks,MingW with Windows. Last edited by Ducky; 11-03-2009 at 10:38 PM. |
| Ducky is offline | |
| | #18 |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,120
| 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.
__________________ "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008 |
| cpjust is offline | |
| | #19 | ||
| The larch Join Date: May 2006
Posts: 3,082
| Quote:
Code: string s = &v[0];
__________________ I might be wrong. Quote:
| ||
| anon is offline | |
| | #20 |
| Registered User Join Date: Dec 2007 Location: France
Posts: 396
| 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.
__________________ Using Code::Blocks,MingW with Windows. |
| Ducky is offline | |
| | #21 |
| Registered User Join Date: Dec 2007 Location: France
Posts: 396
| 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;
}
__________________ Using Code::Blocks,MingW with Windows. |
| Ducky is offline | |
| | #22 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| Yes, because with the code you have, there is no guarantee that &vec[0] is null terminated.
__________________ bit∙hub [bit-huhb] n. A source and destination for information. |
| bithub is offline | |
| | #23 |
| Registered User Join Date: Dec 2007 Location: France
Posts: 396
| Thank you Bithub! Can it be done simpler than this? Code: str1 += &vec[0]; str1 += '\0';
__________________ Using Code::Blocks,MingW with Windows. Last edited by Ducky; 11-04-2009 at 12:26 PM. |
| Ducky is offline | |
| | #24 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| You need to null terminate the vector before you add it to the string, not after. A push_back('\0') on the vector before adding it to the string should do the trick.
__________________ bit∙hub [bit-huhb] n. A source and destination for information. |
| bithub is offline | |
| | #25 |
| Registered User Join Date: Jan 2005
Posts: 7,137
| 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. |
| Daved is offline | |
| | #26 |
| Registered User Join Date: Dec 2007 Location: France
Posts: 396
| Thanks Daved! You are revealing details about vectors that doesnt really explained anywhere. Interesting.
__________________ Using Code::Blocks,MingW with Windows. |
| Ducky is offline | |
| | #27 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,381
| 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: Code: std::string foo( vec.begin(), vec.end() ); Code: std::vector< char > foo( str.begin(), str.end() );
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot |
| brewbuck is offline | |
| | #28 |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,120
| 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).
__________________ "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008 |
| cpjust is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Unable to compare string with 'getter' returned string. | Swerve | C++ Programming | 2 | 10-30-2009 05:56 PM |
| [Inheritance Hierarchy] User Input on program with constructors. How ? | chandreu | C++ Programming | 8 | 04-25-2008 02:45 PM |
| Personal Program that is making me go wtf? | Submeg | C Programming | 20 | 06-27-2006 12:13 AM |
| Character arrays | PsychoBrat | C++ Programming | 7 | 06-21-2002 12:02 PM |
| How do you search & sort an array? | sketchit | C Programming | 30 | 11-03-2001 05:26 PM |