【转】char*,const char*和string的相互转换
1. string转const char*
string s = "abc";
const char* c_s = s.c_str();
2. const char*转string
直接赋值即可
const char* c_s = "abc";
string s(c_s);
3. string转char*
string s = "abc";
char* c;
const int len = s.length();
c = new char[len+1];
strcpy(c,s.c_str());
4. char*转string
char* c = "abc";
string s(c);
5. const char*转char*
const char* cpc = "abc";
char* pc = new char[100];//足够长
strcpy(pc,cpc);
6. char*转const char*
直接赋值即可
char* pc = "abc";
const char* cpc = pc;
原文地址:http://blog.sina.com.cn/s/blog_5436b2f40100pjzz.html
2011-4-2 00:32
char * ch = const_cast<char*>(str.c_str())
2012-11-2 12:16
【转】char*,const char*和string的相互转换的更多相关文章
- (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的相互转换
好久没写东西啦,发表学术文章一篇,hiahia~ 近日和小佳子编程时遇到很多转换问题,很麻烦,在网上查了很多资料. 为了以后查找方便,特此总结如下. 如果有不对的地方或者有更简单的方法,请指出~~ 1 ...
- string char * const char *之间的互相转换
string -> const char * 用str的c_str()方法或者data()方法均可,这个两个方法返回值为cong char * string str = "hel ...
- 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'; ...
- wchar_t与char、wstring与string的相互转换
个人倾向于使用优秀的开源库做这个. 最近使用boost进行转换,代码极其简单: boost::filesystem::path src(wchar_t); char = src.string().c_ ...
- const char*, char const*, char*const的区别
http://www.cnblogs.com/aduck/articles/2244884.html
随机推荐
- 转载:Character data is represented incorrectly when the code page of the client computer differs from the code page of the database in SQL Server 2005
https://support.microsoft.com/en-us/kb/904803 Character data is represented incorrectly when the cod ...
- codeforces 579D D. "Or" Game(前后缀+贪心)
题目链接: D. "Or" Game time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Simofox 2.7 - 基于 pcxFirefox 定制(停更)
••• 现已停止更新,无良作者转战 Google Chrome ••• 项目名称:Simofox (Simple + Cool + Firefox) 项目介绍:Simofox 中译名西蒙狐,目前项目版 ...
- 浅谈实现placeholder效果的几种方案
placeholder是html5<input>的一个属性,它提供可描述输入字段预期值的提示信息(hint), 该提示会在输入字段为空时显示.高端浏览器支持此属性(ie10/11在获得焦点 ...
- Python(2.7.6) 异常类的继承关系
BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exception +-- StopIteration ...
- Spring事务传递性探讨
本篇主要讨论下面几点获取[下载地址] : 一: Spring 事务的传递性介绍 二: 第三方调用含有事务的Service抛异常方法探讨 一: Spring 事务的传递性介绍 事务传播行为,所谓事务的 ...
- Mysql部分常用类型长度含义
Int:一个Int类型4字节 在sql中长度为1则代表一个Int类型的长度 有符号区分的范围:2147483647~-214 ...
- 运行第一个Node.js程序
初学Node.js,萌新一枚.感觉Node.js挺不错的,从基础开始一步一步来,加油吧! 我们来使用Node.js的express来运行第一个程序helloworld: 在命令提示符键入express ...
- CSS之密码强度检测
输入密码后单击空白处即可检测. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht ...
- 获取一年时间的sql
select a.day, to_char(a.day, 'day') as dd, 1 as flag,to_char(a.day,'YYYY-MM-DD') from ( SELECT TO_DA ...