C++ int转string(stringstream可转更多类型)
一、使用atoi
说明:
itoa( int value, char *string, int radix );
第一个参数:你要转化的int;
第二个参数:转化后的char*;
第三个参数:你要转化的进制;
举例:
//-------------------------------------
//功能:C++ int 转 string (使用atoi)
//环境:VS2005
//-------------------------------------
#include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int n = ;
char c[];
itoa(n, c, );
cout << "2-> " << c << endl;
itoa(n, c, );
cout << "16-> " << c << endl;
itoa(n, c, );
cout << "10-> " << c << endl;
system("pause");
return ;
}
输出:
2-> 11110
16-> 30
10-> 1e
二、使用sprintf
头文件 #include<stdio.h>
语法: int sprintf(string format, mixed [args]...);
返回值:字符串长度(strlen)
转换字符
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
% 印出百分比符号,不转换。
b 整数转成二进位。
c 整数转成对应的 ASCII 字元。
d 整数转成十进位。
f 倍精确度数字转成浮点数。
o 整数转成八进位。
s 整数转成字串。
x 整数转成小写十六进位。
X 整数转成大写十六进位。
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
举例:
//-------------------------------------
//功能:C++ int 转 string (使用sprintf)
//环境:VS2005
//-------------------------------------
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n = ;
char c[];
sprintf(c, "%d", n);
cout << c << endl;
sprintf(c, "%o", n);
cout << c << endl;
sprintf(c, "%X", n);
cout << c << endl;
sprintf(c, "%c", n);
cout << c << endl;
float f = 24.678;
sprintf(c, "%f", f);
cout << c << endl;
sprintf(c, "%.2f", f);
cout << c << endl;
sprintf(c, "%d-%.2f", n, f);
cout << c << endl;
system("pause");
return ;
}
输出:
30
36
1E//注:这里是个特殊符号
24.677999
24.68
30-24.68
三、使用stringstream
Linux下编译通过的通用模板(int,double,char[]通过,推荐):
/*
convert other data to string
usage :
string str = m_toStr<int>(12345);
*/
template <class T> string m_toStr(T tmp)
{
stringstream ss;
ss << tmp;
return ss.str();
}
其他例子:
//-------------------------------------
//功能:C++ int 转 string (使用stringstream)
//环境:VS2005
//-------------------------------------
#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
stringstream strStream;
int a = 100;
float f = 23.5566;
strStream << a << "----"<< f ;
string s = strStream.str();
cout << s << endl;
system("pause");
return 0;
}
输出:
100----23.5566
四、其它
1.sprintf可能引起缓冲区溢出,可以考虑使用 snprintf 或者非标准的 asprintf
2.如果是mfc程序,可以使用 CString::Format
3.如果使用boost,则可以直接使用: string s = boost::lexical_cast <string>(a);
4.atoi 也是不可移植的。
五、其它NB方法
//-----------------------------------------------------------------------------------
// 参考引用 :
// http://baike.baidu.com/view/982195.htm?fr=ala0_1_1
// http://baike.baidu.com/view/1295144.htm?fr=ala0_1
// http://pppboy.blog.163.com/blog/static/3020379620085511954382/
//-----------------------------------------------------------------------------------
C++ int转string(stringstream可转更多类型)的更多相关文章
- int和string之间的转换
#include<cstring> #include<algorithm> #include<stdio.h> #include<iostream> # ...
- int to string & string to int
#include "stdafx.h" #include <string> #include <sstream> using namespace std; ...
- C++中int转string与string转int
#include "stdafx.h" #include "string" #include "iostream" #include &qu ...
- P1980 计数问题(int,string,stringstream)
题目描述 试计算在区间 1 到 n 的所有整数中,数字x(0 ≤ x ≤ 9)共出现了多少次?例如,在 1 到 11 中,即在 1,2,3,4,5,6,7,8,9,10,11 中,数字 1 出现了 4 ...
- C++ 中 int 转string, 以及10进制转2进制
感谢:http://blog.csdn.net/xiaofei2010/article/details/7434737 以及:http://www.cnblogs.com/nzbbody/p/3504 ...
- int and string
int转string一.#include <sstream> int n = 0; std::stringstream ss; std::string str; ss<<n; ...
- C++ int与string的转化
int本身也要用一串字符表示,前后没有双引号,告诉编译器把它当作一个数解释.缺省情况下,是当成10进制(dec)来解释,如果想用8进制,16进制,怎么办?加上前缀,告诉编译器按照不同进制去解释.8进制 ...
- C++里的int 和string类型相互转换
C++不像Java和C#一样在进行数据类型转换时直接调用一些类方法就可以了,使用起来很简单. 一个很简单的例子就是string str=“D:\\”+1+“.txt”;这在Java或者C#里面是可以自 ...
- C++ int转string
一.使用atoi 说明: itoa( int value, char *string, int radix ); 第一个参数:你要转化的int; 第 ...
随机推荐
- [py]python中__new__作用
元类metaclass 使劲搞,但是没搞清楚__new__的作用 了解Python元类 Python进阶:一步步理解Python中的元类metaclass Python中的__new__和__init ...
- 配置Tomcat直接显示目录结构和文件列表
Tomcat是直接显示目录结构和文件列表,只是在配置里面给关闭了. 关键在这里:\conf\web.xml 这个文件有一段配置直接控制Tomcat是允许显示目录结构和文件列表. <servlet ...
- centos7 lua安装
yum -y install gcc automake autoconf libtool makeyum install readline-develcurl -R -O http://www.lua ...
- 198. House Robber(动态规划)
198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...
- VS2010/MFC编程入门之十二(对话框:非模态对话框的创建及显示)
上一节鸡啄米讲了模态对话框及其弹出过程,本节接着讲另一种对话框--非模态对话框的创建及显示. 鸡啄米已经说过,非模态对话框显示后,程序其他窗口仍能正常运行,可以响应用户输入,还可以相互切换.鸡啄米会将 ...
- 2017-2018 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2017) Solution
A - Airport Coffee 留坑. B - Best Relay Team 枚举首棒 #include <bits/stdc++.h> using namespace std; ...
- C Strange Sorting
C. Strange Sorting time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- this指向 - 总结
/* 总结: this 的指向: 1.this 的指向 是在执行上下文时才确定的, 并且确定后不可更改: 2.this 指向 “其执行上下文的环境对象”; “其执行上下文的环境对象” 判读依据如下: ...
- 20145325张梓靖 《Java程序设计》第3周学习总结
20145325张梓靖 <Java程序设计>第3周学习总结 教材学习内容总结 类与对象 类,就相当于设计图纸,用"new"创建的对象,就是依据设计图做成的成品:设计图纸 ...
- CPA定律——一致性,可用性和分区容错性
按照美国著名科学家 Eric Brewer 在 2000 年提出的理论,当技术架构从集中式架构向分布式架构演进,会遇到 “CAP 定律”的瓶颈. CAP 说明一个数据处理系统不能同时满足一致性,可用性 ...