string :: c_str()和string :: data()之间有什么区别? [英] What's the difference between string::c_str() and string::data()?
问题描述
在< basic_string.h>中,我找到了这两个
函数的实现。但我无法理解它们之间的区别。
请给我一些帮助!
// basic_string :: c_str()
const _CharT *
c_str()const
{
// MT:这假设并发写入正常。
size_type __n = this-> size();
traits_type :: assign(_M_data()[_ _ n],_ Rep :: _ S_terminal);
返回_M_data() ;
}
// basic_string :: data()
const _CharT *
数据()const {return _M_data(); }
谢谢!
In the <basic_string.h>, I find the implementation of these two
functions. But I can''t understand the difference between them.
Please give me some help!
//basic_string::c_str()
const _CharT*
c_str() const
{
// MT: This assumes concurrent writes are OK.
size_type __n = this->size();
traits_type::assign(_M_data()[__n], _Rep::_S_terminal);
return _M_data();
}
//basic_string::data()
const _CharT*
data() const { return _M_data(); }
Thanks!
推荐答案
Metro12 skrev :
Metro12 skrev:
在< basic_string.h>中,我找到了这两个
函数的实现。但我无法理解它们之间的区别。
请给我一些帮助!
std :: string不以空值终止。虽然两个函数都返回一个
指向字符数组的指针,但是std :: string :: c_str()确保
数组以\0结尾。
/ Peter
[snip]谢谢!
In the <basic_string.h>, I find the implementation of these two
functions. But I can''t understand the difference between them.
Please give me some help!
A std::string is not null-terminated. While both functions return a
pointer to an array of the characters, std::string::c_str() make sure
that the array ends with \0.
/Peter
[snip] Thanks!
2005年8月23日00:46:33 -0700,Metro12 < sa ******* @ yahoo.com.cn>
写道:
On 23 Aug 2005 00:46:33 -0700, "Metro12" <sa*******@yahoo.com.cn>
wrote:
在< basic_string.h>中,我发现执行这两个功能。但我无法理解它们之间的区别。
请给我一些帮助!
// basic_string :: c_str()
const _CharT *
c_str()const
{
// MT:这假设并发写入正常。
size_type __n = this-> size();
traits_type :: assign(_M_data()[__ n], _Rep :: _ S_terminal);
返回_M_data();
}
// basic_string :: data()
const _CharT *
data() const {return _M_data(); }
谢谢!
In the <basic_string.h>, I find the implementation of these two
functions. But I can''t understand the difference between them.
Please give me some help!
//basic_string::c_str()
const _CharT*
c_str() const
{
// MT: This assumes concurrent writes are OK.
size_type __n = this->size();
traits_type::assign(_M_data()[__n], _Rep::_S_terminal);
return _M_data();
}
//basic_string::data()
const _CharT*
data() const { return _M_data(); }
Thanks!
C ++标准保证一个是空终止的,另一个是
不是(但可能是)。
-
Bob Hairgrove
没有********** @ Home.com
周二,23日2005年8月09:55:35 +0200,Bob Hairgrove
< in ***** @ bigfoot.com>写道:
On Tue, 23 Aug 2005 09:55:35 +0200, Bob Hairgrove
<in*****@bigfoot.com> wrote:
在< basic_string.h>中,我找到了这两个
函数的实现。但是我无法理解它们之间的区别。
In the <basic_string.h>, I find the implementation of these two
functions. But I can''t understand the difference between them.
[snip]
C ++标准保证一个是空终止的,另一个是
不是(但可能是)。
[snip]
One is guaranteed by the C++ standard to be null-terminated, the other
isn''t (but might be).
另外,因为std :: string可以包含二进制数据,包括null
字符,data()函数在那些
情况下会更有用。 c_str(),OTOH返回的指针保证以空值终止,并且通常用于处理
文本字符串的遗留代码。
虽然看起来在任何一种情况下都可以使用c_str(),但是根据实现情况,
data()可能不会以null结尾。
< br $> b $ b -
Bob Hairgrove
不** ********@Home.com
这篇关于string :: c_str()和string :: data()之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!