C++中const char*, string 与char*的转化
原文:https://blog.csdn.net/zhang_alongzd/article/details/52790905
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhang_alongzd/article/details/52790905
1:string转化成const char*
可以通过调用string类中的c_str函数实现string向const char*的转换
- //string与const char*转换
- string s = "string_To_const char* ";
- const char *c_s=s.c_str();
- const char *c_ss = s.data();
还可以直接使用string类中的data()函数,直接对const char *赋值
但是c_s并不是不变的,如果在之后改变了s的内容,c_s仍然会随着改变,这样就会造成不可弥补的错误!所以需要重新定义变量开内存管理c_s指针;
2:const char*转换成string利用string的一个构造函数对string类的对象赋值即可
- //const char*转换string
- const char* p = "const char* _To_string";
- string y(p);
3:string与char*的转换
string不能直接转换为char*,需要通过其他的方法来实现,现在简单介绍几种方法:
1:通过函数strcpy来实现;
- //string与char*的转换
- string ppp = "stringTochar*";
- char* c;
- const int len = ppp.length();
- c=new char[len+1];
- strcpy(c,ppp.c_str());
- string ppp = "stringTochar*";
- char *d;
- d=new char[len+1];
- *(d+len)='\0';
- ppp.copy(d,len,0);
- //char*与string的转换
- char* pp = "stringTochar*";
- string q(pp);
5:const char*转化成char*
- //const char*转化成char*
- const char* csc = "constchar*Tochar*";
- char* cc;
- int length = strlen(csc);
- cc = new char[length+1];
- strcpy(cc,csc);
- cout<<cc<<" "<<csc<<endl;
- // string.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include<iostream>
- #include<string>
- #pragma warning(disable:4996)
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- //string与const char*转换
- string s = "string_To_constchar*";
- const char *c_s=s.c_str();
- const char *c_ss = s.data();
- cout<<s<<" "<<c_s<<" "<<c_ss<<endl;
- //const char*转换string
- const char* p = "constchar*_To_string";
- string y(p);
- cout<<p<<" "<<y<<endl;
- //char*与string的转换
- char* pp = "stringTochar*";
- string q(pp);
- cout<<pp<<" "<<q<<endl;
- //string与char*的转换
- string ppp = "stringTochar*";
- char* c;
- const int len = ppp.length();
- c=new char[len+1];
- strcpy(c,ppp.c_str());
- char *d;
- d=new char[len+1];
- *(d+len)='\0';
- ppp.copy(d,len,0);
- cout<<ppp<<" "<<c<<" "<<d<<endl;
- //const char*转化成char*
- const char* csc = "constchar*Tochar*";
- char* cc;
- int length = strlen(csc);
- cc = new char[length+1];
- strcpy(cc,csc);
- cout<<cc<<" "<<csc<<endl;
- cin.get();
- cin.get();
- return 0;
- }
1:string转化成const char*
可以通过调用string类中的c_str函数实现string向const char*的转换
- //string与const char*转换
- string s = "string_To_const char* ";
- const char *c_s=s.c_str();
- const char *c_ss = s.data();
还可以直接使用string类中的data()函数,直接对const char *赋值
但是c_s并不是不变的,如果在之后改变了s的内容,c_s仍然会随着改变,这样就会造成不可弥补的错误!所以需要重新定义变量开内存管理c_s指针;
2:const char*转换成string利用string的一个构造函数对string类的对象赋值即可
- //const char*转换string
- const char* p = "const char* _To_string";
- string y(p);
3:string与char*的转换
string不能直接转换为char*,需要通过其他的方法来实现,现在简单介绍几种方法:
1:通过函数strcpy来实现;
- //string与char*的转换
- string ppp = "stringTochar*";
- char* c;
- const int len = ppp.length();
- c=new char[len+1];
- strcpy(c,ppp.c_str());
- string ppp = "stringTochar*";
- char *d;
- d=new char[len+1];
- *(d+len)='\0';
- ppp.copy(d,len,0);
- //char*与string的转换
- char* pp = "stringTochar*";
- string q(pp);
5:const char*转化成char*
- //const char*转化成char*
- const char* csc = "constchar*Tochar*";
- char* cc;
- int length = strlen(csc);
- cc = new char[length+1];
- strcpy(cc,csc);
- cout<<cc<<" "<<csc<<endl;
- // string.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include<iostream>
- #include<string>
- #pragma warning(disable:4996)
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- //string与const char*转换
- string s = "string_To_constchar*";
- const char *c_s=s.c_str();
- const char *c_ss = s.data();
- cout<<s<<" "<<c_s<<" "<<c_ss<<endl;
- //const char*转换string
- const char* p = "constchar*_To_string";
- string y(p);
- cout<<p<<" "<<y<<endl;
- //char*与string的转换
- char* pp = "stringTochar*";
- string q(pp);
- cout<<pp<<" "<<q<<endl;
- //string与char*的转换
- string ppp = "stringTochar*";
- char* c;
- const int len = ppp.length();
- c=new char[len+1];
- strcpy(c,ppp.c_str());
- char *d;
- d=new char[len+1];
- *(d+len)='\0';
- ppp.copy(d,len,0);
- cout<<ppp<<" "<<c<<" "<<d<<endl;
- //const char*转化成char*
- const char* csc = "constchar*Tochar*";
- char* cc;
- int length = strlen(csc);
- cc = new char[length+1];
- strcpy(cc,csc);
- cout<<cc<<" "<<csc<<endl;
- cin.get();
- cin.get();
- return 0;
- }