首页 > 其他 > 详细

string类的data() 和c_str()区别

时间:2017-04-01 19:09:47      阅读:297      评论:0      收藏:0      [点我收藏+]

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_t l2 = s.size();
13     const char* p1 = s.data();
14     const char* p2 = s.c_str();
15     cout << l1 <<" "    << l2 << " "<< p1 << " "    << p2 << " "     <<endl;
16     cout <<" sizeof( p1 )" << sizeof( p1 ) <<endl;
17     cout << " strlen( p1 ) " << strlen(p1) <<endl;
18     cout <<" sizeof( p2 )" << sizeof( p2 ) <<endl;
19     cout << " strlen( p2 ) " << strlen(p2) <<endl;
20     cout << "p1 = " << p1 << "\np2 = "<<p2 <<endl; 
21     printf("p1 = %p \n p2 = %p \n",p1,p2);
22     return 0;
23 }

 结果如下:                         编译器gcc4.4.7 centos6.5  

技术分享

从使用效果上可以初步判断一样。

 

2. 查看源代码,如下图:

 技术分享

目前看,还是有不同的, _res 的赋值不同。   但是_Base::data() 是什么? _Base 这个类不知道怎么找,待更新。

 

有知道的小伙伴欢迎评论,指点;

 

string类的data() 和c_str()区别

原文:http://www.cnblogs.com/csun/p/6657138.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!