C++基础---string类的data/c_str/copy

本文详细介绍了C++标准库中string类的三种转换方法:data、c_str及copy。通过示例代码展示了如何将string对象转换为C风格字符串,并讨论了它们在实际应用中的差异。

1.string类的data/c_str/copy

1.1 std::string::data

  • 原型: const char *data()const;
  • 功能: string型字符串转化为char型字符串。
  • 说明:将自身转化为一个以空终止符结束(即带”\0“结尾)的char型字符串。
  • 返回值:一个指向字符串数组的指针,字符串数组指针。
  • 代码示例:

    (1)示例代码一:
    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str = "Test string";
        char *cstr = "Test string";
    
        if (str.length() == strlen(cstr))
        {
            cout<<"str and cstr have the same length."<<endl;
    
            if (memcmp(cstr, str.data(), str.length()) == 0)
            {
                cout<<"str and cstr have the same content."<<endl;
            }
        }
    
        system("pause");
        return 0;
    }
    =>str and cstr have the same length.
      str and cstr have the same content.
    (2)示例代码二:
    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str("Please split this sentence into tokens");
        const char *split = " ";
        char *cstr = new char [str.length()+1];
        strcpy_s(cstr, str.size() + 1, str.data());
    
        //cstr now contains a c-string copy of str
        char *p = 0;
        char *pNext = 0;
        p = strtok_s(cstr, split, &pNext);
        while(p != 0)
        {
            cout<<p<<endl;
            p = strtok_s(NULL, split, &pNext);
        }
    
        delete[] cstr;
        system("pause");
    
        return 0;
    }   
    =>Please
      split
      this
      sentence
      into
      tokens

1.2 std::string::c_str

  • 原型:const char *c_str()const;
  • 功能: string型字符串转化为char型字符串。
  • 说明:将自身转化为一个以空终止符结束(即带”\0“结尾)的char型字符串。
  • 返回值:一个指向字符串数组的指针,字符串数组指针。
  • 代码示例:

    (1)示例代码一:
    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str = "Test string";
        char *cstr = "Test string";
    
        if (str.length() == strlen(cstr))
        {
            cout<<"str and cstr have the same length."<<endl;
    
            if (memcmp(cstr, str.c_str(), str.length()) == 0)
            {
                cout<<"str and cstr have the same content"<<endl;
            }
        }
    
        system("pause");
        return 0;
    }
    =>str and cstr have the same length.
      str and cstr have the same content.
    (2)示例代码二:
    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        string str("Please split this sentence into tokens");
        const char *split = " ";
        char *cstr = new char [str.length()+1];
        strcpy_s(cstr, str.size() + 1, str.c_str());
    
        //cstr now contains a c-string copy of str
        char *p = 0;
        char *pNext = 0;
        p = strtok_s(cstr, split, &pNext);
        while (p != 0)
        {
            cout<<p<<endl;
            p = strtok_s(NULL, split, &pNext);
        }
    
        delete[] cstr;
        system("pause");
    
        return 0;
    }
    =>Please
      split
      this
      sentence
      into
      tokens

1.3 std::string::copy

  • 原型:size_t copy (char* s, size_t len, size_t pos = 0) const;
  • 功能: string型字符串转化为char型字符串。
  • 说明:从源字符串以下标为pos(默认为0)处开始拷贝n个字符到char型字符串s中。
  • 返回值:实际拷贝的字符个数。
  • 代码示例:

    
    #pragma warning(disable:4996) //全部关掉
    
    
    #include <iostream>
    
    
    #include <string>
    
    using namespace std;
    int main ()
    {
        char buffer[20];
        string str("Test string...");
        size_t length = str.copy(buffer, 6, 5);
        buffer[length]='\0';
        cout<<"buffer contains: "<<buffer<<endl;
        system("pause");
        return 0;
    }
    =>buffer contains: string

参考文献:
[1] 网络资源: http://www.cplusplus.com/reference/string/string/data/
       http://www.cplusplus.com/reference/string/string/c_str/
       http://www.cplusplus.com/reference/string/string/copy/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值