C++中int型与string型互相转换(转)
http://greatverve.cnblogs.com/archive/2012/10/24/cpp-int-string.html
本以为这么多年C#经验,学个C++没多难,现在发现错了。C++真TM难。
今天遇到int转string绊了半天,方法很多,不知道为什么搞那么复杂,
我只挑最简单易懂的,管他效率不效率的。
int转string
int n = 0;
std::stringstream ss;
std::string str;
ss<<n;
ss>>str;
string转int
std::string str = "123";
int n = atoi(str.c_str());
#include "stdafx.h" #include <string>
#include <sstream> using namespace std;
void main()
{
// int 转 string
stringstream ss;
int n = 123;
string str;
ss<<n;
ss>>str;
// string 转 int
str = "456";
n = atoi(str.c_str());
}
url: http://greatverve.cnblogs.com/archive/2012/10/24/cpp-int-string.html
参考:
#include <string> //使用C++标准库的string类时
using namespace std; //同上
#include <sstream>
#include <iostream>
#include <stdlib.h> //要将string类和int类型直接转换最好有这些包含,
//因为自己写一个转换函数比较方便,函数定义参考如下
string getstring ( const int n )
{
std::stringstream newstr;
newstr<<n;
return newstr.str();
}
string 转 CString
CString.format(”%s”, string.c_str());
char 转 CString
CString.format(”%s”, char*);
char 转 string
string s(char *);
string 转 char *
char *p = string.c_str();
CString 转 string
string s(CString.GetBuffer());
1,string -> CString
CString.format(”%s”, string.c_str());
用c_str()确实比data()要好.
2,char -> string
string s(char *);
只能初始化,在不是初始化的地方最好还是用assign().
3,CString -> string
string s(CString.GetBuffer());
GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.
《C++标准函数库》中说的
有三个函数可以将字符串的内容转换为字符数组和C—string
1.data(),返回没有”\0“的字符串数组
2,c_str(),返回有”\0“的字符串数组
3,copy()
—————————————————————
CString与int、char*、char[100]之间的转换- -
CString与int、char*、char[100]之间的转换- -
CString互转int
将字符转换为整数,可以使用atoi、_atoi64或atol。
而将数字转换为CString变量,可以使用CString的Format函数。如
CString s;
int i = 64;
s.Format(”%d”, i)
Format函数的功能很强,值得你研究一下。
void CStrDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString
ss=”1212.12″;
int temp=atoi(ss);
CString aa;
aa.Format(”%d”,temp);
AfxMessageBox(”var is ” + aa);
}
sart.Format(”%s”,buf);
CString互转char*
///char * TO cstring
CString strtest;
char * charpoint;
charpoint=”give string a value”;
strtest=charpoint;
///cstring TO char *
charpoint=strtest.GetBuffer(strtest.GetLength());
标准C里没有string,char *==char []==string
可以用CString.Format(”%s”,char *)这个方法来将char *转成CString。要把CString转成char *,用操作符(LPCSTR)CString就可以了。
CString转换 char[100]
char a[100];
CString str(”aaaaaa”);
strncpy(a,(LPCTSTR)str,sizeof(a));
C++中int型与string型互相转换(转)的更多相关文章
- C字符串和C++中string的区别 &&&&C++中int型与string型互相转换
在C++中则把字符串封装成了一种数据类型string,可以直接声明变量并进行赋值等字符串操作.以下是C字符串和C++中string的区别: C字符串 string对象(C++) 所需的头文件名称 ...
- PDO 查询mysql返回字段整型变为String型解决方法
PDO 查询mysql返回字段整型变为String型解决方法 使用PDO查询mysql数据库时,执行prepare,execute后,返回的字段数据全都变为字符型. 例如id在数据库中是Int的,查询 ...
- java中int转成String位数不足前面补零
java中int转成String位数不足前面补零 转载自:http://ych0108.iteye.com/blog/2174134 java中int转String位数不够前面补零 String.fo ...
- C字符串和C++中string的区别 &&&&C++中int型与string型互相转换
在C++中则把字符串封装成了一种数据类型string,可以直接声明变量并进行赋值等字符串操作.以下是C字符串和C++中string的区别: C字符串 string对象(C++) 所需的头文件名称 ...
- Android中 int,float,Double,String 互相转换
1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt( ...
- 【JAVA】java中int转成String位数不足前面补零例如:1->001
String.format("%03d", 1); 0代表前面要补的字符3代表字符串长度d表示参数为整数类型 测试完数据:循环了100次 截取了一部分:
- Java 中的 int 型转为 long 型
先将 int 型转为 String 型,然后再将 String 转为 long 型,如下图: public class TestIntToLong { public static void main( ...
- STL_string.【转】C++中int、string等常见类型转换
ZC:#include <sstream> ZC:貌似还有 istringstream 和 ostringstream ... https://www.cnblogs.com/gaobw/ ...
- C++中int、string等常见类型转换
1.int型与string型的互相转换 最佳实践: int型转string型 void int2str(const int &int_temp,string &string_temp) ...
随机推荐
- sql语句出错:Column count doesn't match value count at row 1
报错内容: java.sql.SQLException: Column count doesn't match value count at row 1 at com.mysql.jdbc.SQLEr ...
- 以ActiveMQ为例JAVA消息中间件学习【4】——消息中间件实际应用场景
前言 当前真正学习消息中间件,当前已经走到了,可以简单的使用,网上有很多那种复杂的高可用的架构,但是那些都是对于一些比较大型的项目来说的. 对于一些小型的项目可能用不到那么大的架构,于是我们需要从最简 ...
- C#中List的方法RemoveAt小测试
结论:在C#中将一个List中的项插入到别一个List中,会复制,而不是从源List中移除. 示例如下 void Start () { TestList (); } void TestList () ...
- GTest的安装与使用
安装GTest 1.安装源代码 下载gtest,release-1.8.0 git clone https://github.com/google/googletest gtest编译 cd goog ...
- 基于 CGLIB 库的动态代理机制
之前的文章我们详细的介绍了 JDK 自身的 API 所提供的一种动态代理的实现,它的实现相对而言是简单的,但是却有一个非常致命性的缺陷,就是只能为接口中的方法完成代理,而委托类自己的方法或者父类中的方 ...
- AD预测论文研读系列2
EARLY PREDICTION OF ALZHEIMER'S DISEASE DEMENTIA BASED ON BASELINE HIPPOCAMPAL MRI AND 1-YEAR FOLLOW ...
- 使用 libjpeg 库解压数据示例
static int MjpegDecompress( *** ) { struct jpeg_decompress_struct tDInfo; struct jpeg_error_mgr jerr ...
- NABCD校园生活
N(Need,需求) 在校师生得到的信息分散,极大的影响了师生的学习效率,所以就会有快速得到全面信息的需求,而我们的APP正是在解决这个问题. A(Approach,做法) 我们会在主页进行信息分类, ...
- vb.net Function使用
Public Function colour(ByVal Code As Int16) As Color'顏色 Select Case Code Case 1 colour = Color.White ...
- Java学习笔记之——类与对象
1.参数的传递方式 1)值传递 2)引用传递 2.类和对象: (1)类的定义: public class 类名{ 类型 属性1: 类型 属性2: ……… public 返回值类型 方法名1(形参){ ...