zoukankan      html  css  js  c++  java
  • 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 << "
    p2 = "<<p2 <<endl; 
    21     printf("p1 = %p 
     p2 = %p 
    ",p1,p2);
    22     return 0;
    23 }

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

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

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

     

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

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

  • 相关阅读:
    Unix/Linux笔记全集
    深入浅出-变长参数
    基于 SSH 的远程操作以及安全,快捷的数据传输<转>
    面向对象的特性—— 封装
    wpf 窗体翻页效果
    wpf控件拖动
    Wpf 导出CSV文件
    wpf 导出Excel
    Wpf Button 样式
    wpf简单进度条
  • 原文地址:https://www.cnblogs.com/csun/p/6657138.html
Copyright © 2011-2022 走看看