std :: string和char指针 [英] std::string and char pointers
问题描述
假设我有一个char指针和一个std :: string。是否有可能获得指向std :: string'的内容的
指针。这样char指针可以指向
相同的文本?反之亦然;我可以给std :: string一个指针和一个
长度然后给std :: string控制指针及其
内容吗?
我基本上试图避免在std :: string和
a char指针之间复制大文本,反之亦然。
std :: string类中有没有支持这个?
,Espen
Espen Ruud Schultz写道:让我说我有一个char指针和一个std :: string。是否有可能获得指向std :: string'的内容的指针。这样char指针可以指向相同的文本?
是的。阅读''std :: string :: c_str()''方法。但无论如何,
''std :: string''不会将存储字符串的控制权移交给
字符指针。 ''std :: string''将保留对存储的
字符串的完全控制,并可能使前一次调用返回的指针无效
'c_str()'' 。
反之亦然;我可以给std :: string一个指针和一个
长度然后给std :: string控制指针及其内容吗?
编号''std :: string''会复制。
我基本上试图避免在std之间复制大文本: :string和
一个char指针,反之亦然。
这种情况下最好的策略是单独使用''std :: string''尝试
避免使用字符串拥有由char指针。
-
祝你好运,
Andrey Tarasevich
Brainbench C和C ++编程MVP
2003年7月18日星期五13:22:09 -0700,Andrey Tarasevich
< an * *************@hotmail.com>写道:
Espen Ruud Schultz写道:让我说我有一个char指针和一个std :: string。是否有可能获得指向std :: string'的内容的指针。这样char指针可以指向相同的文本吗?
是的。阅读''std :: string :: c_str()''方法。但无论如何,
不!当然你可以从一个字符串中得到一个char const *,但是在你使用它之后它不会保证是好的。如果你使用c_str(),
你必须立即使用该指针,然后扔掉它。如果你想要保存指针并稍后使用它,你*可能*有一个狂野的
指针。
Long故事简短,你需要深层复制字符串。为什么你想要一个
char *指向一个std :: string缓冲区呢?重复数据是
*通常*一个错误。
21.3.6.2州:
需要:[在任何后续调用非const
函数之后,p [rogram]也不会将重新调整的值视为
有效指针值... [snip] < br>反之亦然;我可以给std :: string一个指针和一个
长度然后给std :: string控制指针及其内容吗?
否。 ''std :: string''将复制。
对,但你可以使用auto_ptr而不是字符串。
您想要完成的是什么?
< / dib>
John Dibling
电子邮件: dib@substitute_my_full_last_name_here.com
为保护而省略了戏弄性的玩笑
On Fri,2003年7月18日21:49:47 +0200,Espen Ruud Schultz
< de ***** @ nospam。无效>写道:
让我说我有一个字符指针和一个std :: string。是否有可能获得指向std :: string'的内容的指针。这样char指针可以指向相同的文本?反之亦然;我可以给std :: string一个指针和一个
长度然后给std :: string控制指针及其内容吗?
我基本上试图避免在std :: string和/或char指针之间复制大文本,反之亦然。
std :: string类中是否有任何支持这个?>
,Espen
std :: string.c_str()
Olli
Lets say I have a char pointer and an std::string. Is it possible to get a
pointer to the std::string''s "content" so that the char pointer can point to
the same text? And vice versa; can I give the std::string a pointer and a
length and then give the std::string control over the pointer and its
content?
I''m basically trying to avoid copying large text between an std::string and
a char pointer, and vice versa.
Is there anyhing in the std::string class to support this?
, Espen
Espen Ruud Schultz wrote:Lets say I have a char pointer and an std::string. Is it possible to get a
pointer to the std::string''s "content" so that the char pointer can point to
the same text?
Yes. Read about ''std::string::c_str()'' method. But in any case,
''std::string'' will not hand over control of the stored string to the
char pointer. ''std::string'' will retain full control of the stored
string and may invalidate the pointer returned by a previous call to
''c_str()''.
And vice versa; can I give the std::string a pointer and a
length and then give the std::string control over the pointer and its
content?
No. ''std::string'' will make a copy.
I''m basically trying to avoid copying large text between an std::string and
a char pointer, and vice versa.
The best strategy in this case is to use ''std::string'' alone and try to
avoid using strings "owned" by char pointers.
--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP
On Fri, 18 Jul 2003 13:22:09 -0700, Andrey Tarasevich
<an**************@hotmail.com> wrote:
Espen Ruud Schultz wrote:Lets say I have a char pointer and an std::string. Is it possible to get a
pointer to the std::string''s "content" so that the char pointer can point to
the same text?
Yes. Read about ''std::string::c_str()'' method. But in any case,
No! Sure you can get a char const* from a string, but it''s not
guaranteed to be any good after youv''e used it. If you use c_str(),
you have to use that pointer right away, then throw it away. If you
try to save the pointer and use it later, you *could* have a wild
pointer.
Long story short, you need to deep-copy the string. Why do you want a
char* to point to a std::string buffer anyway? Duplication of data is
*usually* a bug.
21.3.6.2 states:
Requires: [snip] Nor shall the p[rogram treat the retuned valuie as a
valid pointer value after any subsequent call to a non-const
function... [snip]
And vice versa; can I give the std::string a pointer and a
length and then give the std::string control over the pointer and its
content?
No. ''std::string'' will make a copy.
Right, but you could use an auto_ptr instead of a string.
Just what is it you are trying to accomplish?
</dib>
John Dibling
email: dib@substitute_my_full_last_name_here.com
Witty banter omitted for your protection
On Fri, 18 Jul 2003 21:49:47 +0200, "Espen Ruud Schultz"
<de*****@nospam.invalid> wrote:
Lets say I have a char pointer and an std::string. Is it possible to get a
pointer to the std::string''s "content" so that the char pointer can point to
the same text? And vice versa; can I give the std::string a pointer and a
length and then give the std::string control over the pointer and its
content?
I''m basically trying to avoid copying large text between an std::string and
a char pointer, and vice versa.
Is there anyhing in the std::string class to support this?
, Espen
std::string.c_str()
Olli
这篇关于std :: string和char指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!