c++ string char* const char*

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main()
{
    {
        string s = "tom and jerry";
        const char* c_s = s.c_str();
        cout << "---------------" << endl;
        cout << c_s << endl;
        cout << s << endl;
    }

    {
        const char* c_s = "tom and jerry";
        string s(c_s);
        cout << "---------------" << endl;
        cout << c_s << endl;
        cout << s << endl;
    }

    {
        string s = "tom and jerry";
        char* c;
        const int len = s.length();
        c = new char[len+1];
        strcpy(c, s.c_str()); //#include <cstring>
        cout << "---------------" << endl;
        cout << c << endl;
        cout << s << endl;
    }


    {
        char* c = "tom and jerry";
        string s(c);
        cout << "---------------" << endl;
        cout << c << endl;
        cout << s << endl;
    }

    {
        const char* c = "tom and jerry";
        char* pc = new char[100];
        strcpy(pc, c);
        cout << "---------------" << endl;
        cout << c << endl;
        cout << pc << endl;
    }


    return 0;
}
原文地址:https://www.cnblogs.com/i80386/p/4387128.html