c++ string c_str() 和data()区别 const char * c_str()const; 获取等效的C字符串 生成一个以空终止的字符序列(c-string),其内容与字符串对象相同,并将其作为指向字符数组的指针返回,且在最后位置有个附加的空终止字符。 const char * data()const;...
std::string的⽅法c_str()和data()有什么区别1、从C++标准上的解释来看,只有⼀点区别:c_str() 返回⼀个指向正规C字符串的指针常量,该指针保证指向⼀个 size() + 1 长度的空间,⽽且最后⼀个字符肯定是 \0 ;⽽ data() 返回的指针则保证指向⼀个size()长度的空间,不保证有没有null...
std::string str ="hello";// 在C++98中是未定义行为autop = &str[0] 好在C++11标准中意识到了这一点,规定string底层存储的字符串直接采用C风格的字符串语法,所以data()和c_str()就变成了同义词了: std::string str ="hello";// 在C++11中是正确的,p指向一个空字符串autop = &str[0]...
string类的data() 和c_str()区别 1、 首先想到的就是代码测试下(百度之后的) 1 #include <string> 2 #include <iostream> 3 #include <string.h> 4 #include <stdio.h> 5 6 using namespace std; 7 8 int main() 9 { 10 string s = "123 456"; 11 size_t l1 = s.length(); 12 size_...
深入探讨C++中std::string类的成员函数c_str()和data()的区别,需要从历史角度出发。在过去,string类设计时并不强制内部存储以'\0'结尾的字符串,这与现代语言的设计趋势相悖。为此,在C++11标准发布之前,仅通过调用string::c_str()函数才能获取到以'\0'结尾的字符串。然而,C++11标准进行了改进...
标准库的string类提供了3个成员函数来从一个string得到c类型的字符数组:c_str()、data()、copy(p, n)。 1. c_str():生成一个const char*指针,指向以空字符终止的数组。 注: ①这个数组的数据是临时的,当有一个改变这些数据的成员函数被调用后,其中的数据就会失效。因此要么现用先转换,要么把它的数据复...
Get string data Returns a pointer to an array of characters with the same content as the string. Notice that no terminating null character is appended (see member c_str for such a functionality). c_str()字符串后有'\0',而data()没有。
const value_type *c_str( ) const; const value_type *data( ) const; data只是返回原始数据序列,没有保证会用traits::eos(),或者说'\0'来作字符串结束 ...
标准库的string类提供了3个成员函数来从一个string得到c类型的字符数组:c_str()、data()、copy(p,n)。 c_str()是Borland封装的String类中的一个函数,它返回当前字符串的首字符地址。换种说法,c_str()函数返回一个指向正规C字符串的常量指针(不是指针常量),内容与本string串相同。这是为了与C语言兼容,在C...
c_str()anddata()perform the same function. (since C++ 11)所以在这之后两个函数效果相同。事实上...