从std :: string转换为char *,有更好的方法吗? [英] Conversion from std::string to char *, is there a better way?

查看:68
本文介绍了从std :: string转换为char *,有更好的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重新评估我从std :: string转换为char *的方式。

(要求:源是std :: string,可用内容是char *)


以下是我的想法:


#include< string>

#include< vector>

#include< cstring>


//从某处推测s,例如:

std :: string s ="< initial value>" ;;


std :: vector< char> v(s.length()+ 1);

std :: strcpy(& v [0],s.c_str());

char * c = & v [0];

//使用c,其中char *是_specifically_ required

s = c;


以上:

- 不使用任何必须手动解除分配的指针

- 100%便携(?)

- 是100%符合(?)

我见过Bjarne的类似实现,但是它使用了新的char *而不是

vector(显然是写的)在STL被采用之前)。


auto_ptr< char>代替矢量< char>不起作用,因为它没有处理

阵列。


有没有人对上述内容有任何意见,特别是对于它的适用性/>
定义的要求是什么?

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?

推荐答案

2004年6月10日星期四10:48:33 -0700,Julie< ju * **@nospam.com>写道:
On Thu, 10 Jun 2004 10:48:33 -0700, Julie <ju***@nospam.com> wrote:
我正在重新评估我从std :: string转换为char *的方式。
(要求:源是std ::字符串,可用的内容是char *)

这就是我想出的:

#include< string>
#include<向量>
#include< cstring>

//从某处推测s,例如:
std :: string s ="< initial value>" ;;

std :: vector< char> v(s.length()+ 1);
std :: strcpy(& v [0],s.c_str());
char * c =& v [0];


使用括号可能是一个好主意(即:&(v [0])而不是

& v [0])。 ..我永远不会记住运营商的订单

优先。

//使用c,其中char *是_specifically_ required
s = c; <上面的内容:
- 没有使用任何必须手动取消分配的指针
- 100%便携(?)
- 100%符合(?我已经看过Bjarne的类似实现,但它使用了新的char *而不是
vector(显然是在采用STL之前编写的)。

的auto_ptr<炭>代替矢量< char>不起作用,因为它没有处理数组。

有没有人对上述内容有任何意见,特别是它是否适合定义的要求?
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];
Might be a good idea to use parentheses (i.e.: &(v[0]) instead of
&v[0] here) ... I can never remember the order of the operator
precedence.
// 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?




给我看起来没问题。


std :: string :: c_str()将返回一个const char *指向

字符串'的字符数据,你可以用来传递给函数,这些函数是
期望指向零分隔的C风格字符串的常量指针

参数。这是访问角色数据的最简单方法。


OTOH,如果你有一个期望非const char *的函数,你必须复制
必须复制c_str()返回的常量缓冲区中的数据到

非常量缓冲区,通常使用new

char [s.size()]分配例如,。抛弃

const可能是危险的,如果函数除了读取数据之外什么都做了(行为是实际上未定义的b $ b)。


当然,你的代码在使用缓冲区完成

时必须取消分配内存。我总是使用一个智能指针,可以处理数组

数据(即智能指针调用的析构函数delete []和

不是删除) 。 Boost库具有这样的智能指针模板。我使用我自己的一个,我改编自代码grin pointer,

但是Boost库预计将被合并到下一个

版本的C ++标准。


向量< char>也是一个很好的解决方案,虽然可能比使用一个简单的

字符数组更多开销(当然不是很多)。幸运的是,该标准确保

向量元素必须在内存中连续,因此将&(v [0])分配给

a char *即可。

-

Bob Hairgrove
No *** *******@Home.com


Julie写道:
我正在重新评估方式我从std :: string转换为char *。
(要求:源是std :: string,可用内容是char *)

这是我''我们提出:

#include< string>
#include< vector>
#include< cstring>

//推测s来自某个地方,例如:
std :: string s ="< initial value>" ;;

std :: vector< char> v(s.length()+ 1);
std :: strcpy(& v [0],s.c_str());
char * c =& v [0];
//使用c,其中char *是_specifically_ required
s = c;

以上:
- 不使用必须手动的任何指针取消分配
- 100%便携(?)


看起来好像

- 100%符合(?)


一旦''std :: vector''存储

的连续性要求使其成为标准(如果它还没有)并且逐渐渗透到

所有的库实现(如果他们还没有完成它已经开始

了)。

我见过Bjarne'的类似的实现,但它使用新的char *而不是
vector(显然在采用STL之前编写)。


不,它是在有人建议std :: vector'的

存储需要连续之前写的。

有没有人对上述内容有任何意见,特别是它是否适合定义的要求?
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(?)
Looks like it
- is 100% conformant(?)
As soon as the requirement for contiguousness of ''std::vector'' storage
makes it into the Standard (if it hasn''t already) and trickles down to
all library implementations (if they haven''t done it already to begin
with).

I''ve seen Bjarne''s similar implementation, but it uses new char * instead of
vector (obviously written before the STL was adopted).
No, it was written before there was a suggestion that std::vector''s
storage needs to be contiguous.
Does anyone have comments on the above, specifically as to its suitability for
the requirements defined?




AFAICT,代码没问题。


V



AFAICT, the code is fine.

V


Victor Bazarov写道:
Victor Bazarov wrote:
- 100%符合(?)
- is 100% conformant(?)



一旦''std :: vector''存储的连续性要求使其成为标准(如果它没有'已经没有了,并且逐渐渗透到所有的库实现中(如果他们还没有开始使用它)。


As soon as the requirement for contiguousness of ''std::vector'' storage
makes it into the Standard (if it hasn''t already) and trickles down to
all library implementations (if they haven''t done it already to begin
with).




只是为了消除任何不确定性,请参阅第23.2.4 / 1节:


向量的元素是连续存储的,这意味着如果v是

a vector< T,Allocator>其中T是bool以外的某种类型,那么它b / b
服从所有0< = n<< = n<< = n< v.size()。"

Alan



Just to remove any uncertainty here, from Section 23.2.4/1 :

"The elements of a vector are stored contiguously, meaning that if v is
a vector<T, Allocator> where T is some type other than bool, then it
obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size()."
Alan


这篇关于从std :: string转换为char *,有更好的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆