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_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 这个类不知道怎么找,待更新。

 

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

 

转载于:https://www.cnblogs.com/csun/p/6657138.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值