std :: string有类似CString :: GetBuffer的东西吗? [英] does std::string have something like CString::GetBuffer?

查看:133
本文介绍了std :: string有类似CString :: GetBuffer的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




i需要这个,因为Windows的路径函数,比如PathAppend

和PathRemoveFileExt接受一个可写的零终止char *,但我是/>
没有找到std :: string,CString,我通常使用

GetBuffer


LPTSTR CString: :GetBuffer(int nMinBufLength)

解决方案

On Fri,2008年5月30日03:43:45 -0700,sas写道:





i需要这个,因为windows的路径函数,比如PathAppend和

PathRemoveFileExt接受一个可写零终止char *,但我没有
找到std :: string,用CString,我通常使用GetBuffer for




LPTSTR CString :: GetBuffer(int nMinBufLength)



const char * std :: string :: c_str()const


-

Lionel B


5月30日,2:23 * pm,Lionel B< m ... @ privacy.netwrote:


On Fri,2008年5月30日03:43:45 -0700,sas写道:


hi,


i需要因为路径的功能,如PathAppend和

PathRemoveFileExt接受一个可写的零终止字符*,但我没有找到bd的字符串,使用CString,我通常使用GetBuffer作为

表示


LPTSTR CString :: GetBuffer(int nMinBufLength)



const char * std :: string :: c_str()const


-

Lionel B



c_str对我不起作用,因为它返回一个const,我希望是/ b $ b能够将原始的零终止缓冲区传递给一个C函数

更改它,然后告诉字符串对象使用新的

seq进行更新例如:


int a = 5;

CString str;

char * buffer = str.GetBuffer(MAX_PATH);

sprintf(buffer," var =%d",a);

str.ReleaseBuffer() ;


str现在是var = 5


带字符串我必须这样做:


int a = 5;

std :: string str;

char buffer [MAX_PATH];

strcpy(buffer,str .c_str());

sprintf(缓冲区,var =%d,a);

str =缓冲区;


i必须为std :: string使用额外的缓冲区


On Fri,2008年5月30日05:41:45 -0700,sas写道:


5月30日,2:23?* pm,Lionel B< m ... @ privacy.netwrote:


> On Fri,2008年5月30日03:43:45 -0700,sas写道:


hi,


i需要因为windows的路径函数,比如PathAppend

和PathRemoveFileExt接受一个可写的零终止char *,但是我没有找到std ::字符串,CString,我通常使用

GetBuffer为


LPTSTR CString :: GetBuffer(int nMinBufLength)


const char * std :: string :: c_str()const

- Lionel B



^^^^^^^^

(请不要引用sigs)


c_str不适合我,因为它返回一个const,我希望能够将原始的零终止缓冲区传递给改变它的C函数,

然后告诉字符串对象用新序列更新。



不,当然。你不能写到c_str()一个std :: string。它是

的一部分std :: string的内部并且绝对是只读的而不是

搞砸了;因此const。


例如:


int a = 5;

CString str ;

char * buffer = str.GetBuffer(MAX_PATH); sprintf(buffer," var =%d",a);

str.ReleaseBuffer();


str现在是var = 5

带字符串的
我必须这样做:


int a = 5;

std :: string str ;

char buffer [MAX_PATH];

strcpy(buffer,str.c_str());



^^^^^^^^^^^^^^^^^^^^^^^^^^^

这看起来很危险(甚至编译?)并且无论如何都是毫无意义的,因为你后来设置了string = buffer,所以
。把它留下来吧。


sprintf(buffer," var =%d",a);

str = buffer;


i必须为std :: string使用额外的缓冲区



是的,如果你想更新std ::确实如此来自C风格的字符串

字符串。实际上,并不能保证std :: string甚至可以使用char缓冲区实现,而不是
,否则为零终止或




-

Lionel B


hi,

i need that because the path functions for windows, like PathAppend
and PathRemoveFileExt accept a writable zero terminated char*, but i
didn''t find that for std::string, with CString, i usually use
GetBuffer for that

LPTSTR CString::GetBuffer( int nMinBufLength )

解决方案

On Fri, 30 May 2008 03:43:45 -0700, sas wrote:

hi,

i need that because the path functions for windows, like PathAppend and
PathRemoveFileExt accept a writable zero terminated char*, but i didn''t
find that for std::string, with CString, i usually use GetBuffer for
that

LPTSTR CString::GetBuffer( int nMinBufLength )

const char* std::string::c_str() const

--
Lionel B


On May 30, 2:23*pm, Lionel B <m...@privacy.netwrote:

On Fri, 30 May 2008 03:43:45 -0700, sas wrote:

hi,

i need that because the path functions for windows, like PathAppend and
PathRemoveFileExt accept a writable zero terminated char*, but i didn''t
find that for std::string, with CString, i usually use GetBuffer for
that

LPTSTR CString::GetBuffer( int nMinBufLength )


const char* std::string::c_str() const

--
Lionel B

c_str doesn''t work for me, because it returns a const, i want to be
able to pass the raw zero-terminated buffer to a C function that
changes it, then tell the string object to update with the new
sequence.

for example:

int a = 5;
CString str;
char* buffer = str.GetBuffer(MAX_PATH);
sprintf(buffer, "var = %d", a);
str.ReleaseBuffer();

str is now "var = 5"

with string i have to do this:

int a = 5;
std::string str;
char buffer[MAX_PATH];
strcpy(buffer, str.c_str());
sprintf(buffer, "var = %d", a);
str = buffer;

i have to use an additional buffer for std::string


On Fri, 30 May 2008 05:41:45 -0700, sas wrote:

On May 30, 2:23?*pm, Lionel B <m...@privacy.netwrote:

>On Fri, 30 May 2008 03:43:45 -0700, sas wrote:

hi,

i need that because the path functions for windows, like PathAppend
and PathRemoveFileExt accept a writable zero terminated char*, but i
didn''t find that for std::string, with CString, i usually use
GetBuffer for that

LPTSTR CString::GetBuffer( int nMinBufLength )


const char* std::string::c_str() const

--
Lionel B

^^^^^^^^
(please don''t quote sigs)

c_str doesn''t work for me, because it returns a const, i want to be able
to pass the raw zero-terminated buffer to a C function that changes it,
then tell the string object to update with the new sequence.

No, sure. You can''t "write to the c_str()" of a std::string. It''s part of
the internals of a std::string and is definitely read-only and not to be
messed about with; hence the const.

for example:

int a = 5;
CString str;
char* buffer = str.GetBuffer(MAX_PATH); sprintf(buffer, "var = %d", a);
str.ReleaseBuffer();

str is now "var = 5"

with string i have to do this:

int a = 5;
std::string str;
char buffer[MAX_PATH];
strcpy(buffer, str.c_str());

^^^^^^^^^^^^^^^^^^^^^^^^^^^

This looks dangerous (does it even compile?) and is pointless anyway,
since you later set string = buffer. Just leave it out.

sprintf(buffer, "var = %d", a);
str = buffer;

i have to use an additional buffer for std::string

Yes you do indeed if you want to update a std::string from a C-style
string. In fact there''s no guarantee that a std::string is even
implemented with anything like a char buffer, zero-terminated or
otherwise.

--
Lionel B


这篇关于std :: string有类似CString :: GetBuffer的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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