oxygen_728
asked on
C++ - Std::String - char* - writing directly to a std::string's buffer as it were a char*
Given this code:
string A = "FPS: ";
string B(20,'\0');
_gcvt(FPS,5,&B[0]);
A = A + B;
cout << A;
I would like to discuss the best (and safest) way to write to B as if it were a char*
- I've read that it is possible to cast B.begin() to char* and write to it, but I was unable to do this (got some casting error)
- I think it is understood that if a person is going to do this, then their string will be of appropriate size in the first place.
I'm mainly concerned with any unforseen issues (possibly caused by the internal workings of std::string) and as speed (as a minor point)
Thanks in advance for your time
string A = "FPS: ";
string B(20,'\0');
_gcvt(FPS,5,&B[0]);
A = A + B;
cout << A;
I would like to discuss the best (and safest) way to write to B as if it were a char*
- I've read that it is possible to cast B.begin() to char* and write to it, but I was unable to do this (got some casting error)
- I think it is understood that if a person is going to do this, then their string will be of appropriate size in the first place.
I'm mainly concerned with any unforseen issues (possibly caused by the internal workings of std::string) and as speed (as a minor point)
Thanks in advance for your time
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
jkr:
>> BTW, for that what you want to do, I'd rather use
>> stringstream ss;
I've chosen to use std::string so that I can use a pool-memory allocator from boost.org
>> BTW, for that what you want to do, I'd rather use
>> stringstream ss;
I've chosen to use std::string so that I can use a pool-memory allocator from boost.org
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'd like to have the option to write to std::string as if it were a char * so that I can use speedy functions like _gcvt(..) and anything in DirectX that might pop up that needs a char* to write to.
ASKER
">>I've chosen to use std::string so that I can use a pool-memory allocator from boost.org
You can still do that, the stringstream only serves as a conversion helper here. Note the
B = ss.str();
which will hand over the stream's contents to your string, regardless of which allocator you want to use."
Oh, I'm sorry - i misread that code - I thought you declared everything as stringstreams
You can still do that, the stringstream only serves as a conversion helper here. Note the
B = ss.str();
which will hand over the stream's contents to your string, regardless of which allocator you want to use."
Oh, I'm sorry - i misread that code - I thought you declared everything as stringstreams
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
In order to stay on topic, I'm going to post another question regarding the usage of stringstreams and std::string
I'd be curious what other people have to say about this char* business.
I'll post a link in a sec
I'd be curious what other people have to say about this char* business.
I'll post a link in a sec
ASKER
When I said "I'd be curious what other people have to say about this char* business" I meant, that's why I'm going to leave this post open. I appreciate your time and effort jkr.
here's the link to the question regarding stringstream usage in conjunction with std::string https://www.experts-exchange.com/questions/21870170/Using-std-string-in-conjunction-with-stringstream.html
here's the link to the question regarding stringstream usage in conjunction with std::string https://www.experts-exchange.com/questions/21870170/Using-std-string-in-conjunction-with-stringstream.html
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'd like to avoid creating char[] buffers just to hold data-to-be-stored in a std::string ... seems wasteful