Conversion from std::string to char *, is there a better way?
From: Julie (julie_at_nospam.com)
Date: 06/10/04
- Next message: Mike Wahler: "Re: need help with casting operators..."
- Previous message: Brendan Grant: "Communications Breakdown with CSocket"
- Next in thread: Bob Hairgrove: "Re: Conversion from std::string to char *, is there a better way?"
- Reply: Bob Hairgrove: "Re: Conversion from std::string to char *, is there a better way?"
- Reply: Victor Bazarov: "Re: Conversion from std::string to char *, is there a better way?"
- Reply: Dave Townsend: "Re: Conversion from std::string to char *, is there a better way?"
- Reply: Siemel Naran: "Re: Conversion from std::string to char *, is there a better way?"
- Reply: red floyd: "Re: Conversion from std::string to char *, is there a better way?"
- Reply: Jorge Rivera: "Re: Conversion from std::string to char *, is there a better way?"
- Reply: Julie: "Re: Conversion from std::string to char *, is there a better way?"
- Reply: Julie: "Re: Conversion from std::string to char *, is there a better way?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 10 Jun 2004 10:48:33 -0700
I'm re-evaluating the way that I convert from a std::string to char *.
(Requirement: the source is a std::string, the usable contents are char *)
Here is what I've come up with:
#include <string>
#include <vector>
#include <cstring>
// presume s from somewhere, such as:
std::string s = "<initial value>";
std::vector<char> v(s.length() + 1);
std::strcpy(&v[0], s.c_str());
char * c = &v[0];
// use c, where a char * is _specifically_ required
s = c;
The above:
- doesn't use any pointers that must be manually deallocated
- is 100% portable(?)
- is 100% conformant(?)
I've seen Bjarne's similar implementation, but it uses new char * instead of
vector (obviously written before the STL was adopted).
auto_ptr<char> in place of vector<char> doesn't work because it doesn't handle
arrays.
Does anyone have comments on the above, specifically as to its suitability for
the requirements defined?
- Next message: Mike Wahler: "Re: need help with casting operators..."
- Previous message: Brendan Grant: "Communications Breakdown with CSocket"
- Next in thread: Bob Hairgrove: "Re: Conversion from std::string to char *, is there a better way?"
- Reply: Bob Hairgrove: "Re: Conversion from std::string to char *, is there a better way?"
- Reply: Victor Bazarov: "Re: Conversion from std::string to char *, is there a better way?"
- Reply: Dave Townsend: "Re: Conversion from std::string to char *, is there a better way?"
- Reply: Siemel Naran: "Re: Conversion from std::string to char *, is there a better way?"
- Reply: red floyd: "Re: Conversion from std::string to char *, is there a better way?"
- Reply: Jorge Rivera: "Re: Conversion from std::string to char *, is there a better way?"
- Reply: Julie: "Re: Conversion from std::string to char *, is there a better way?"
- Reply: Julie: "Re: Conversion from std::string to char *, is there a better way?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|