C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-03-2009, 07:22 PM   #16
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,120
Ah, of course. Using resize() & size() are correct.

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   Reply With Quote
Old 11-03-2009, 09:54 PM   #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   Reply With Quote
Old 11-04-2009, 08:26 AM   #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   Reply With Quote
Old 11-04-2009, 08:43 AM   #19
The larch
 
Join Date: May 2006
Posts: 3,082
Quote:
Apparrently its working without the '\0'.
At least i dont have garbage at the end and nothing missing either.
If things like the following work:
Code:
string s = &v[0];
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).
__________________
I might be wrong.

Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Quoted more than 1000 times (I hope).
anon is offline   Reply With Quote
Old 11-04-2009, 10:43 AM   #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   Reply With Quote
Old 11-04-2009, 10:58 AM   #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   Reply With Quote
Old 11-04-2009, 11:18 AM   #22
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
Quote:
Originally Posted by Ducky View Post
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;
}
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   Reply With Quote
Old 11-04-2009, 12:19 PM   #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   Reply With Quote
Old 11-04-2009, 12:26 PM   #24
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
Quote:
Originally Posted by Ducky View Post
Thank you Bithub!

Is it ok like this too?

Code:
 str1 += '\0';
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   Reply With Quote
Old 11-04-2009, 12:31 PM   #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   Reply With Quote
Old 11-04-2009, 02:35 PM   #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   Reply With Quote
Old 11-04-2009, 03:45 PM   #27
Senior software engineer
 
brewbuck's Avatar
 
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() );
Convert string to vector of char:

Code:
std::vector< char > foo( str.begin(), str.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.
__________________
"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   Reply With Quote
Old 11-04-2009, 08:49 PM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:35 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22