【转载】char*,const char*和string 三者转换
本文转自 http://blog.csdn.net/perfumekristy/article/details/7027678
const char* 和string 转换
- const char*转换为 string,直接赋值即可
EX: const char* tmp = "hello world!".
string s = tmp;
- string转换为const char*,利用c_str()
EX: string s = "hello world";
const char*tmp = s.c_str();
char*和const char*之间的转换
- const char*转化为char*,利用const_cast
EX: const char* tmp = "hello world";
char* p = const_cast<char*>(tmp);
- char*转化为const char*,直接赋值即可。
char* p = "hello world".
const char* tmp = p;
char*和string之间的转换
有了上述两点的基础,char*和string转化就很简单了。
- char*转化为string,直接赋值即可。
EX: char* p = "hello world".
string str = p;
- string转化为char*,走两步,先是string->const char*,然后是const char*->char*
EX: string str = "hello world";
char* p = const_cast<char*>(str.c_str());
【转载】char*,const char*和string 三者转换的更多相关文章
- string char * const char *之间的互相转换
string -> const char * 用str的c_str()方法或者data()方法均可,这个两个方法返回值为cong char * string str = "hel ...
- 【转】char*,const char*和string的相互转换
1. string转const char* string s = "abc"; const char* c_s = s.c_str(); 2. const char*转string ...
- char*,const char*和string的相互转换
好久没写东西啦,发表学术文章一篇,hiahia~ 近日和小佳子编程时遇到很多转换问题,很麻烦,在网上查了很多资料. 为了以后查找方便,特此总结如下. 如果有不对的地方或者有更简单的方法,请指出~~ 1 ...
- (c++) int 转 string,char*,const char*和string的相互转换
一.int 和string的相互转换 1 int 转化为 string c++ //char *itoa( int value, char *string,int radix); // 原型说明: / ...
- char*,const char*和string 三者转换
1. const char* 和string 转换 (1) const char*转换为 string,直接赋值即可. EX: const char* tmp = "tsinghua&quo ...
- c++ string char* const char*
#include <iostream> #include <string> #include <cstring> using namespace std; int ...
- char*,const char*和string 互转
1. string转const char* 1 string s = "abc"; 2 const char* c_s = s.c_str(); 2. const char*转st ...
- char const*, char*const, const char *const的区别
C++标准规定,const关键字放在类型或变量名之前等价的.所以,const char*和 char const*是一样的. const char* //常量指针---指向常量的指针----指针指 ...
- 常量, char[], const char[], char*, const char*, char* const以及const char* const的详解
注意,这里用char类型只是举了一个例子,其他的int之类的也通用. 1: 常量: 例子: char str[] = "Hello world!"; char ch = 'a'; ...
随机推荐
- C#中正则表达式的构建与匹配
使用方法 [1]用用命名空间System.Text.RegularExpressions [2]构造正则表达式 在使用正则表达式时,要先构造正则表达式,这就用到了Regex类,其构建方式有两种: 基本 ...
- asp.net 翻页时用ViewState保存上一页checkbox勾选的值
/// <summary> /// checkbox勾选取消勾选事件 /// </summary> /// <param nam ...
- The sixteenth day
It is a against the law to drive without a driver's license 翻译: 没有驾照,开车是违法的 注意点: 1.It(连读)is; witout( ...
- Struts2_HelloWorld_7_2
第一个程序的流程图: Struts2.x 的作用:把请求和展现分开.
- Moodle-3.1.2 (Ubuntu 16.04 )
平台: Ubuntu 类型: 虚拟机镜像 软件包: moodle-3.1.2 commercial education moodle open-source 服务优惠价: 按服务商许可协议 云服务器费 ...
- ssh代理登录内网服务器
服务器 192.168.48.81 # client 192.168.48.82 # bastion 192.168.48.83 # private password方式 192.168.48.81 ...
- [转]用jwplayer+Nginx搭建视频点播服务器,解决拖动加载慢的问题
flv视频可以采用两种方式发布: 一.普通的HTTP下载方式 二.基于Flash Media Server或Red5服务器的rtmp/rtmpt流媒体方式. 多数知名视频网站都采用的是前一种方式. 两 ...
- 基于ASP.NET WPF技术及MVP模式实战太平人寿客户管理项目开发(Repository模式)
亲爱的网友,我这里有套课程想和大家分享,假设对这个课程有兴趣的.能够加我的QQ2059055336和我联系. 课程背景 本课程是教授使用WPF.ADO.NET.MVVM技术来实现太平人寿保险有限公司 ...
- Spring boot 实现高吞吐量异步处理(适用于高并发场景)
技术要点 org.springframework.web.context.request.async.DeferredResult<T> 示例如下: 1. 新建Maven项目 asy ...
- CXF报错[1 counts of IllegalAnnotationExceptions]and[Two classes have the same XML type name]and[Use @XmlType.name and @XmlType.namespace to assign different names to them]
启动时CXF报错如下: Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalA ...